Pagini recente » Cod sursa (job #2841797) | Cod sursa (job #705142)
Cod sursa(job #705142)
#include<iostream>
#include<fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n,t[100001],h[100001];
int find(int x){
while(t[x]){
x=t[x];
}
return x;
}
void Union(int x, int y){
int tx=find(x),ty=find(y);
if(h[tx]<h[ty])
t[tx]=ty;
else{
t[ty]=tx;
if(h[tx]==h[ty])
h[tx]++;
}
}
int main(){
int m,cod,x,y;
in>>n>>m;
while(m--){
in>>cod>>x>>y;
if(cod==1)
Union(x,y);
else
if(find(x)==find(y))
out<<"DA"<<endl;
else
out<<"NU"<<endl;
}
return 0;
}