Cod sursa(job #1340868)

Utilizator beldeabogdanBogdan Beldea beldeabogdan Data 12 februarie 2015 09:10:08
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>
#define nmax 100000
using namespace std;

int n,m;
int t[nmax];

int getA(int x)
{
    while (t[x] != x) x = t[x];
    return x;
}

void set(int t2,int b)
{
    while (t[b] != b)
    {
        int pastB = b;
        b = t[b];
        t[pastB] = t2;
    }
    t[b] = t2;
}

int main()
{
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    scanf("%d %d",&n,&m);
    for (int i=1;i<=n;i++) t[i] = i;
    for (int qi=1;qi<=m;qi++)
    {
        int type,a,b;
        scanf("%d %d %d",&type,&a,&b);

        if (type == 1)
        {
            int t1 = getA(a);
            set(t1,b);
        }
        else
        {
            int t1 = getA(a);
            int t2 = getA(b);
            if (t1 == t2)
            {
                printf("DA\n");
            }
            else
            {
                printf("NU\n");
            }
        }
    }
}