Pagini recente » Cod sursa (job #186675) | Cod sursa (job #2949705) | Cod sursa (job #2000545) | Cod sursa (job #1366253) | Cod sursa (job #643956)
Cod sursa(job #643956)
#include <cstdio>
#define file_in "disjoint.in"
#define file_out "disjoint.out"
#define nmax 101000
int N,M,i,tip,x,y,tata[nmax],nr[nmax],t1,t2;
int find(int x){
if (x!=tata[x])
tata[x]=find(tata[x]);
return tata[x];
}
void unite(int i, int j){
if (nr[i]>nr[j]){
tata[j]=i;
nr[j]+=nr[i];
}
else{
tata[i]=j;
nr[i]+=nr[j];
}
}
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;
nr[i]=1;
}
while(M--){
scanf("%d %d %d", &tip, &x, &y);
if (tip==1){
//uneste
t1=find(x);
t2=find(y);
unite(t1,t2);
}
else
if (find(x)==find(y))
printf("DA\n");
else
printf("NU\n");
}
return 0;
}