Pagini recente » Monitorul de evaluare | Istoria paginii utilizator/upgradestrike | Concursuri Virtuale | Diferente pentru fmi-no-stress-3/solutii intre reviziile 13 si 12 | Cod sursa (job #2000147)
#include <cstdio>
using namespace std;
int t[100001];
int h[100001];
int FindSet(int x)
{
while(t[x]!=x)
{
x=t[x];
}
return x;
}
void UnionSet (int x, int y)
{
if(h[x]==h[y])
{
t[y]=x;
h[x]++;
}
else
if(h[x]<h[y])
{
t[x]=y;
}
else
if(h[x]>h[y])
{
t[y]=x;
}
}
int main()
{
freopen("disjoint.in","r",stdin);
freopen("disjoint.out","w",stdout);
int n,m,cod,x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
{t[i]=i;h[i]=1;}
for(int i=1;i<=m;++i)
{
scanf("%d%d%d",&cod,&x,&y);
if(cod==1)
{
x=FindSet(x);
y=FindSet(y);
if(x!=y)
UnionSet(x,y);
}
else
{
if(FindSet(y)==FindSet(x))
printf("DA\n");
else
printf("NU\n");
}
}
return 0;
}