Pagini recente » Cod sursa (job #748931) | Cod sursa (job #10063) | Cod sursa (job #715749) | Cod sursa (job #2326197) | Cod sursa (job #1889413)
#include <bits/stdc++.h>
#define NMax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int N,M;
int father[NMax],state[NMax];
int GetRoot(int x)
{
int root,y;
for(root=x;root!=father[root];root=father[root]);
while(x!=father[x])
{
y=father[x];
father[x]=root;
x=y;
}
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[x]++;
}
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);
if(rootx!=rooty)
Unite(rootx,rooty);
}
if(op==2)
{
if(GetRoot(x)==GetRoot(y))
fout<<"DA\n";
else
fout<<"NU\n";
}
}
return 0;
}