Pagini recente » Cod sursa (job #3241902) | Clasamentul arhivei de probleme | Cod sursa (job #786358) | Cod sursa (job #2539889) | Cod sursa (job #2474488)
#include<bits/stdc++.h>
#define maxn 100006
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n,m,tata[maxn];
int BIG_DADDY(int nod)
{
int R=nod;
while(tata[R]!=0)
R=tata[R];
while(tata[nod]!=0)
{
int aux=tata[nod];
R=tata[nod];
nod=aux;
}
return R;
}
void unim(int A,int B)
{
A=BIG_DADDY(A);
B=BIG_DADDY(B);
tata[A]=B;
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,cod;
fin>>cod>>x>>y;
if(cod==1)
unim(x,y);
else
{
if(BIG_DADDY(x)==BIG_DADDY(y))
{
fout<<"DA\n";
}
else
fout<<"NU\n";
}
}
return 0;
}