Pagini recente » Cod sursa (job #502071) | Cod sursa (job #1004256) | Cod sursa (job #2027038) | Cod sursa (job #607413) | Cod sursa (job #2367912)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n,m,p[100100],op,x,y;
void init(int n)
{
for(int i=1;i<=n;i++)
p[i]=i;
}
int Find(int x)
{
int r=x;
while(r!=p[r])
r=p[r];
while(p[x]!=r)
{
int t=p[x];
p[x]=r;
x=t;
}
return r;
}
void Union(int x,int y)
{
int r1=Find(x);
int r2=Find(y);
p[r1]=r2;
}
int main()
{
in>>n>>m;
init(n);
for(int i=1;i<=m;i++)
{
in>>op>>x>>y;
if(op==1) Union(x,y);
else if(Find(x)==Find(y)) out<<"DA\n";
else out<<"NU\n";
}
return 0;
}