Pagini recente » Cod sursa (job #1636062) | Cod sursa (job #1875243) | Cod sursa (job #3229087)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int Nmax=100001;
int parent[Nmax];
int get_root(int a)
{
if(a==parent[a])
return a;
parent[a]=get_root(parent[a]);
return parent[a];
}
void set_merge(int a, int b)
{
a=get_root(a);
b=get_root(b);
if(a!=b)
parent[b]=a;
}
int main()
{
int n,m;
fin>>n>>m;
for(int i=1; i<=n; i++) parent[i]=i;
for(int q=1; q<=m; q++)
{
int C,x,y;
fin>>C>>x>>y;
if(C==1)
{
set_merge(x, y);
}
else if(C==2)
{
if(get_root(x)==get_root(y))
fout<<"DA\n";
else fout<<"NU\n";
}
}
return 0;
}