Cod sursa(job #2000147)

Utilizator alexradu04Radu Alexandru alexradu04 Data 12 iulie 2017 18:24:25
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#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;
}