Pagini recente » Cod sursa (job #2793538) | Cod sursa (job #2697970) | Cod sursa (job #2588837) | Cod sursa (job #2428496) | Cod sursa (job #703542)
Cod sursa(job #703542)
#include<fstream>
#include<iostream>
using namespace std;
#define NMAX 100001
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n,t[NMAX],h[NMAX];
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[ty]=tx;
else{
t[tx]=ty;
if(h[tx]==h[ty])
h[ty]++;
}
}
int main(){
int m,x,y,cod;
in>>n>>m;
while(m--){
in>>cod>>x>>y;
if(cod==1) Union(x,y)
else{
if(Find(x)==Find(y))
out<<"DA\n";
else
out<<"NU\n";
}
else
Union(x,y);
}
return 0;
}