Cod sursa(job #1677303)

Utilizator raisacmtAxenie Raisa raisacmt Data 6 aprilie 2016 14:46:38
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <cstdio>

using namespace std;
int t[100005];
int h[100005];
int findset(int x)
{
    while(t[x]!=x)
        x=t[x];
    return t[x];
}
void unionset(int x,int y)
{

    if(h[x]==h[y])
        t[y]=x,h[x]++;
    else
        if(h[x]>h[y])
            t[y]=x;
        else
            t[x]=y;
}
int main()
{
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    int n,m,i,op,a,b,x,y;
    scanf("%d%d",&n,&m);
    for(i=1; i<=n; i++)
        t[i]=i,h[i]=1;
    for(i=1; i<=m; i++)
    {
        scanf("%d%d%d",&op,&a,&b);
        if(op==1)
        {
            x=findset(a);
            y=findset(b);
            unionset(x,y);

        }
        else
        {
            if(findset(a)==findset(b))
                printf("DA\n");
            else
                printf("NU\n");
        }
    }
    return 0;
}