Cod sursa(job #2000640)

Utilizator Andrei_Info1Ionescu Andrei Andrei_Info1 Data 14 iulie 2017 12:05:51
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <cstdio>

using namespace std;
int t[100005], h[100005];
int FindSet(int x)
{
    while(t[x] != x)
        x = t[x];
    return x;
}
void UnionSet(int x, int y)
{
    if(h[x] == h[y])
        h[x]++, t[y] = 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, o, i, x, y;
    scanf("%d%d", &N, &M);
    for(i=1 ; i<=N ; i++)
        h[i]=1, t[i]=i;
    for(i=1 ; i<=M ; i++)
        {
            scanf("%d%d%d", &o, &x, &y);
            if(o == 1)
                UnionSet(x,y);
            else
                if(FindSet(x) == FindSet(y))
                    printf("DA\n");
                else
                    printf("NU\n");
        }
    return 0;
}