Pagini recente » Cod sursa (job #522588) | Cod sursa (job #3276543) | Cod sursa (job #1343774) | Cod sursa (job #248742) | Cod sursa (job #3316048)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, k, tata[100001], cer, x, y;
int comp(int x)
{
int y=x;
while (x!=tata[x])
{
x=tata[x];
}
while (y!=x)
{
int a=tata[y];
tata[y]=x;
y=a;
}
return tata[y];
}
void unify(int x, int y)
{
int x1=comp(x);
int y1=comp(y);
tata[x1]=y1;
}
int main()
{
fin>>n>>k;
for (int i=1; i<=n; i++)
{
tata[i]=i;
}
for (int z=1; z<=k; z++)
{
fin>>cer>>x>>y;
if (cer==1)
{
unify(x, y);
}
else
{
if (comp(x)==comp(y))
{
fout<<"DA"<<'\n';
}
else
{
fout<<"NU"<<'\n';
}
}
}
return 0;
}