Pagini recente » Cod sursa (job #843716) | Cod sursa (job #2792506) | Cod sursa (job #2173192) | Cod sursa (job #1169523) | Cod sursa (job #703426)
Cod sursa(job #703426)
#include<fstream>
using namespace std;
#define NMAX 1000010
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int t[NMAX], h[NMAX],n;
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){
if(Find(x)==Find(y))
out<<"DA\n";
else
out<<"NU\n";
}
else
Union(x,y);
}
return 0;
}