Pagini recente » Cod sursa (job #1699246) | Cod sursa (job #2865945) | Cod sursa (job #2969133) | Cod sursa (job #974247) | Cod sursa (job #2241431)
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int n, m;
int h[100010], tata[100010];
int find(int x)
{
int cx, aux, next;
cx = aux = x;
while(tata[cx])
cx = tata[cx];
while(tata[aux])
{
next = tata[aux];
tata[aux] = cx;
aux = next;
}
return cx;
}
void unite(int x, int y)
{
int px = find(x), py = find(y);
if(h[px] > h[py])
tata[py] = px;
else
tata[px] = py;
if(h[px] == h[py])
++h[py];
}
int main()
{
int c, x, y;
fin>>n>>m;
for(int i=1; i<=m; ++i)
{
fin>>c>>x>>y;
if(c == 1)
unite(x, y);
else
{
if(find(x) == find(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
}
return 0;
}