Pagini recente » Monitorul de evaluare | Cod sursa (job #2364515) | Cod sursa (job #2249891) | Cod sursa (job #1719388) | Cod sursa (job #1615740)
#include <cstdio>
#include <algorithm>
using namespace std;
int a[100001], h[100001], aux;
int find(int x) {
aux=x;
if(a[x]!=x)
aux=find(a[x]);
a[x]=aux;
return aux;
}
void Union(int x, int y) {
int c=find(x), b=find(y);
if(h[c]<h[b])
a[c]=b;
else{
a[b]=c;
if(h[b]==h[c]) h[c]++;
}
}
int main() {
int n, m, cod, x, y, i;
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d%d", &n, &m);
for(i=0; i<n; i++) a[i]=i;
while(m--) {
scanf("%d%d%d", &cod, &x, &y);
if(cod==1)
Union(x,y);
else if(find(x)==find(y)) printf("DA\n");
else printf("NU\n");
}
return 0;
}