Pagini recente » Cod sursa (job #1356044) | Cod sursa (job #961041) | Cod sursa (job #204741) | Cod sursa (job #1134849) | Cod sursa (job #1750881)
#include<stdio.h>
#include<string.h>
using namespace std;
#define FIN "disjoint.in"
#define FOUT "disjoint.out"
#define MAXSIZE 100002
int n,m;
int father[MAXSIZE];
int ranc[MAXSIZE];
int find(int x)
{
if(father[x] == 0)
{
return x;
}
else
{
return find(father[x]);
}
}
int uniune (int x, int y) {
int rooty;
int rootx;
rooty = find(y);
rootx = find(x);
if(rootx == rooty) {
return 0;
}
if(ranc[rootx] > ranc[rooty]) {
father[rooty] = rootx;
} else {
if(ranc[rootx] < ranc[rooty]) {
father[rootx] = rooty;
} else {
father[rootx] = rooty;
ranc[rooty]++;
}
}
}
int main()
{
//INIT: set each element to point to itself
freopen(FIN, "r",stdin);
freopen(FOUT,"w",stdout);
scanf("%d %d", &n,&m);
int x,y,z;
for(int i=1;i<=m;i++)
{
scanf("%d %d %d", &x,&y,&z);
switch(x)
{
case 1:
uniune(y,z);
break;
case 2:
if(find(y) == find(z))
{
printf("DA\n");
}
else printf("NU\n");
break;
}
}
return 0;
}