Pagini recente » Cod sursa (job #2899841) | Cod sursa (job #1817692) | Cod sursa (job #368808) | Cod sursa (job #2116083) | Cod sursa (job #1914085)
#include <bits/stdc++.h>
#define NMax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N,M;
int state[NMax],father[NMax];
int GetRoot(int root)
{
for(;root!=father[root];root=father[root]);
return root;
}
void Unite(int x,int y)
{
if(state[x]>state[y])
father[y]=x;
else
father[x]=y;
if(state[x]==state[y])
state[y]++;
}
int main()
{
fin>>N>>M;
for(int i=1;i<=N;i++)
father[i]=i, state[i]=1;
for(int i=1;i<=M;i++)
{
int op,x,y;
fin>>op>>x>>y;
if(op==1)
{
int rootx=GetRoot(x);
int rooty=GetRoot(y);
Unite(rootx,rooty);
}
if(op==2)
{
int rootx=GetRoot(x);
int rooty=GetRoot(y);
rootx==rooty ? fout<<"DA\n" : fout<<"NU\n";
}
}
return 0;
}