Cod sursa(job #1999504)

Utilizator ialexia_ioanaAlexia Ichim ialexia_ioana Data 11 iulie 2017 12:37:19
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <cstdio>

using namespace std;
int p[100005], h[100005];
int FindSet(int x)
{
    while(p[x]!=x)
        x=p[x];
    return x;
}

void UnionSet(int x, int y)
{
    if (h[x]>=h[y])
    {
        p[y]=x;
        h[x]++;
    }
    else
    {
        p[x]=y;
        h[y]++;
    }
}
int main()
{
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
    int n, m, i, u, v, tu, tv, cod;
    scanf("%d%d", &n, &m);
    for(i=1; i<=n; i++)
        p[i]=i, h[i]=1;
    for(i=1; i<=m; i++)
    {
        scanf("%d%d%d", &cod, &u, &v);
        tu=FindSet(u);
        tv=FindSet(v);
        if (cod==1)
            UnionSet(tu, tv);
        else
            if (tu==tv)
                printf("DA\n");
            else
                printf("NU\n");

    }
    return 0;
}