Pagini recente » Cod sursa (job #3302242) | Cod sursa (job #1401864) | Cod sursa (job #2212382) | Cod sursa (job #1300711) | Cod sursa (job #2285107)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int p[100004],r[100004],i,j,m,n,x,y;
int tata(int x)
{
if (!p[x]) return x;
return tata(p[x]);
}
void uneste(int a,int b)
{
int x=tata(a);
int y=tata(b);
if (x==y) return;
if (r[x]>r[y]) p[y]=x;
if (r[x]<r[y]) p[x]=y;
if (r[x]==r[y]) {p[y]=x; r[x]++;}
}
int main()
{
fin>>n>>m;
for (i=1;i<=m;i++)
{
fin>>j>>x>>y;
if (j==1)
uneste(x,y);
else
if (tata(x)==tata(y)) fout<<"DA\n"; else fout<<"NU\n";
}
return 0;
}