Pagini recente » Cod sursa (job #1637969) | Cod sursa (job #445571) | Cod sursa (job #2738191) | Cod sursa (job #660322) | Cod sursa (job #3267078)
#include <iostream>
#define NMAX 100000
using namespace std;
FILE *in=fopen("disjoint.in", "r"), *out=fopen("disjoint.out", "w");
int n, m, x, y, c;
int root[NMAX+2], height[NMAX+2];
int Find(int x)
{
if(root[x]==x) return x;
return root[x]=Find(root[x]);
}
void Union(int x, int y)
{
x=Find(x);
y=Find(y);
if(x==y) return;
if(height[x]>height[y]) swap(x, y);
root[x]=y;
if(height[x]==height[y])
height[y]++;
}
int main()
{
fscanf(in, "%d %d", &n, &m);
for(int i=1; i<=n; i++)
root[i]=i, height[i]=0;
for(int i=1; i<=m; i++)
{
fscanf(in, "%d %d %d", &c, &x, &y);
if(c==1)
{
Union(x, y);
}
else
{
int nx=Find(x);
int ny=Find(y);
if(nx==ny)
fprintf(out, "DA\n");
else
fprintf(out, "NU\n");
}
}
return 0;
}