Pagini recente » Cod sursa (job #1490622) | Cod sursa (job #1668597) | Cod sursa (job #752090) | Cod sursa (job #2769246) | Cod sursa (job #3229088)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int Nmax=100001;
int parent[Nmax];
int depth[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)
{
if(depth[a]<depth[b])
swap(a, b);
parent[a]=b;
depth[a]=depth[b];
}
}
int main()
{
int n,m;
fin>>n>>m;
for(int i=1; i<=n; i++)
{
parent[i]=i;
depth[i]=1;
}
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;
}