Pagini recente » Cod sursa (job #2265503) | Cod sursa (job #2317525) | Cod sursa (job #2673354) | Cod sursa (job #1011014) | Cod sursa (job #2660030)
#include <bits/stdc++.h>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int arb[100005],v[100005];
int find(int x)
{
if(arb[x]==0)
return x;
return find(arb[x]);
}
int unite(int x,int y)
{
int xp=find(x);
int yp=find(y);
if(v[xp]>=v[yp])
{
while(arb[y])
{
int aux=arb[y];
arb[y]=xp;
y=aux;
}
v[xp]+=v[yp];
arb[yp]=xp;
}
else
{
while(arb[x])
{
int aux=arb[x];
arb[x]=yp;
x=aux;
}
v[yp]+=v[xp];
arb[xp]=yp;
}
}
int main()
{
int n,m;
in>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,cod;
in>>cod>>x>>y;
if(cod==2)
{
if(find(x)==find(y)) out<<"DA"<<"\n";
else out<<"NU"<<"\n";
}
else
{
unite(x,y);
}
}
return 0;
}