Pagini recente » Cod sursa (job #1976901) | Cod sursa (job #642127) | Cod sursa (job #921085) | Cod sursa (job #254316) | Cod sursa (job #601244)
Cod sursa(job #601244)
#include <cstdio>
#include <fstream>
using namespace std;
int p[131072],rg[131072];
int find (int x){
if(x!=p[x])
p[x]=find(p[x]);
return p[x];}
void un (int x,int y){
if(rg[x]>rg[y])
p[y]=x;
else
p[x]=y;
if(rg[x]==rg[y])
++rg[y];
}
int main ()
{
int n,m,t,x,y;
ifstream f ("disjoint.in");
freopen ("disjoint.out","w",stdout);
f>>n>>m;
for(int i=1;i<=n;++i)
p[i]=i;
for(;m;--m){
f>>t>>x>>y;
if(t==1)
un(find(x),find(y));
else
if(find(x)==find(y))
printf("DA\n");
else
printf("NU\n");
}
return 0;}