Pagini recente » Cod sursa (job #1494656) | Cod sursa (job #502703) | Cod sursa (job #298790) | Cod sursa (job #845993) | Cod sursa (job #2711593)
#include <fstream>
#define Nmax 100010
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n,m;
int t[Nmax];
int h[Nmax];
int find(int x)
{ int q=x;
while(q!=t[q]) q=t[q];
while(x!=t[x])
{ int y=t[x];
t[x]=q;
x=y;
}
return q;
}
void uneste(int x,int y)
{ if(h[x]>h[y]) t[y]=x; else t[x]=y;
if(h[x]==h[y]) h[y]
}
int main()
{ cin>>n>>m;
for(int i=1;i<=n;i++) {t[i]=i; h[i]=1;}
for(int op,x,y;m;--m)
{ cin>>op>>x>>y;
if(op==2)
if(find(x)==find(y)) cout<<"DA\n"; else cout<<"NU\n";
else
uneste(find(x),find(y));
}
return 0;
}