Pagini recente » Cod sursa (job #1724169) | Cod sursa (job #2064039) | Cod sursa (job #2096175) | Cod sursa (job #2591830) | Cod sursa (job #1401313)
#include <stdio.h>
#include <stdlib.h>
int sef[100001];
inline int myfind(int pos){
int stiva[100001], st;
st=0;
while(sef[pos]){
stiva[st++]=pos;
pos=sef[pos];
}
while(st>0)
sef[stiva[--st]]=pos;
return pos;
}
inline void myunion(int x, int y){
sef[myfind(y)]=myfind(x);
}
int main()
{
FILE *fin, *fout;
int i, n, m, op, x, y;
fin=fopen("disjoint.in", "r");
fscanf(fin, "%d%d", &n, &m);
fout=fopen("disjoint.out", "w");
for(i=0; i<m; i++){
fscanf(fin, "%d%d%d", &op, &x, &y);
if(op==1)
myunion(x, y);
else if(myfind(x)==myfind(y))
fprintf(fout, "DA\n");
else
fprintf(fout, "NU\n");
}
fclose(fin);
fclose(fout);
return 0;
}