Pagini recente » Cod sursa (job #2190942) | Cod sursa (job #1515943) | Cod sursa (job #2026535) | Cod sursa (job #2169404) | Cod sursa (job #1682270)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define N 100000
using namespace std;
int tat[N];
int findtat(int x){
while(tat[x]>0){
x=tat[x];
}
return x;
}
int main(){
int n,m,i,t;
int x,y,temp;
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++){
tat[i]=-1;
}
for(i=0;i<m;i++){
scanf("%d",&t);
if(t==1){
scanf("%d%d",&x,&y);
x=findtat(x);
y=findtat(y);
if(tat[x]<tat[y]){
tat[x]+=tat[y];
tat[y]=x;
continue;
}
tat[y]+=tat[x];
tat[x]=y;
}else{
scanf("%d%d",&x,&y);
if(findtat(x)==findtat(y)){
printf("DA\n");
continue;
}
printf("NU\n");
}
}
return 0;
}