Pagini recente » Cod sursa (job #1802888) | Cod sursa (job #1713888) | Cod sursa (job #2248666) | Cod sursa (job #2728389) | Cod sursa (job #3165813)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
map <int,int> parent,sizee;
int find_set(int x)
{
if(x==parent[x])
return x;
parent[x]=find_set(parent[x]);
return parent[x];
}
void union_set(int x, int y)
{
x=find_set(x);
y=find_set(y);
if(x==y)
return;
if(sizee[x]<sizee[y])
swap(x,y);
parent[y]=x;
sizee[x]+=sizee[y];
}
int n,m,op,x,y;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
f>>n>>m;
for(int i=1;i<=n;i++)
{
parent[i]=i;
sizee[i]=1;
}
for(int i=1;i<=m;i++)
{
f>>op>>x>>y;
if(op==1)
union_set(x,y);
if(op==2)
{
if(find_set(x)==find_set(y))
g<<"DA"<<'\n';
else
g<<"NU"<<'\n';
}
}
return 0;
}