Pagini recente » Cod sursa (job #2934953) | Cod sursa (job #218407) | Cod sursa (job #315267) | Cod sursa (job #3163778) | Cod sursa (job #703461)
Cod sursa(job #703461)
#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[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;
}