Pagini recente » Cod sursa (job #3366131) | Cod sursa (job #3365127) | Cod sursa (job #3366132) | Cod sursa (job #3366130) | Cod sursa (job #3364695)
#include <bits/stdc++.h>
using namespace std;
const int nm=1e5;
int p[nm+1];
int sz[nm+1];
int Find(int x)
{
if(x==p[x])
{
return x;
}
return p[x]=Find(p[x]);
}
void Union(int x, int y)
{
x=Find(x);
y=Find(y);
if(sz[x]< sz[y])
{
swap(x,y);
}
p[y]=x;
sz[x] += sz[y];
sz[y]= 0;
}
int main()
{
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n, m;
cin >> n >> m;
for (int i=1; i<=n; i++)
{
p[i]=i;
sz[i]=1;
}
for(int i=1; i<=m; i++)
{
int x, y, v;
cin >> v >> x >> y;
if(v==2)
{
if(Find(x)==Find(y))
{
cout << "DA\n";
}
else
{
cout << "NU\n";
}
}
else
{
Union(x, y);
}
}
return 0;
}