Pagini recente » Cod sursa (job #932685) | Cod sursa (job #852237) | Cod sursa (job #2654879) | Cod sursa (job #2134355) | Cod sursa (job #2270306)
#include <iostream>
#include <bits/stdc++.h>
#define N 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int m[N],sz[N];
int n,k;
int Find(int val)
{
int root=val;
while(m[root]!=root)
root=m[root];
int aux;
while(m[val]!=root)
{
aux=m[val];
m[val]=root;
val=aux;
}
return root;
}
void Union(int A,int B)
{
int root_A=Find(A);
int root_B=Find(B);
if(sz[root_A]<sz[root_B])
{
sz[root_B]+=sz[root_A];
m[root_A]=root_B;
}
else
{
sz[root_A]+=sz[root_B];
m[root_B]=root_A;
}
}
void Citire()
{
int i,j,p,x,y;
fin>>n>>k;
for(i=1;i<=n;i++)
{
m[i]=i;
sz[i]=1;
}
for(i=1;i<=k;i++)
{
fin>>p>>x>>y;
if(p==1)
{
if(Find(x)!=Find(y))
Union(x,y);
}
else if(Find(x)==Find(y))
{
fout<<"DA"<<'\n';
}
else fout<<"NU"<<'\n';
}
}
int main()
{
Citire();
return 0;
}