Pagini recente » Cod sursa (job #1387942) | Cod sursa (job #1206149) | Cod sursa (job #2227520) | Cod sursa (job #1606532) | Cod sursa (job #671719)
Cod sursa(job #671719)
#include <cstdio>
#define file_in "disjoint.in"
#define file_out "disjoint.out"
#define nmax 101010
int N,M;
int i,tata[nmax],x,y,tip;
int t1,t2;
int find(int x){
if (x!=tata[x])
tata[x]=find(tata[x]);
return tata[x];
}
int main(){
freopen(file_in,"r",stdin);
freopen(file_out,"w",stdout);
scanf("%d %d", &N, &M);
for (i=1;i<=M;++i) tata[i]=i;
while(M--){
scanf("%d %d %d", &tip, &x, &y);
if (tip==1){
//uneste
t1=find(x);
t2=find(y);
tata[t1]=t2;
}
else
{
//tata lui x
t1=find(x);
//tata lui y
t2=find(y);
if (t1!=t2)//nu sunt in aceasi multime
printf("NU\n");
else//sunt in aceasi multime
printf("DA\n");
}
}
return 0;
}