Pagini recente » Cod sursa (job #72474) | Cod sursa (job #943293) | Cod sursa (job #2206907) | Cod sursa (job #1913413) | Cod sursa (job #703547)
Cod sursa(job #703547)
#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,cod,x,y;
in>>n>>m;
while(m--){
in>>cod>>x>>y;
if(cod==2){
if(Find(x)==Find(y))
out<<"DA\n";
else
out<<"NU\n";
}
else
Union(x,y);
}
return 0;
}