Cod sursa(job #1786778)

Utilizator enescu_rEnescu Radu enescu_r Data 23 octombrie 2016 17:37:42
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <cstdio>
using namespace std;
const int NMAX = 100005;
int x1, y1, N, M, i, op, x, y, t[NMAX];
int tata(int x)
{
    if (x == t[x]) return x;
    else return tata(t[x]);
}

int main()
{
    freopen("disjoint.in", "r", stdin);
    freopen("disjoint.out", "w", stdout);
    scanf("%d%d\n", &N, &M);
    for (i = 1; i <= N; i++)
        t[i] = i;
    for (i = 1; i <= M; i++)
    {
        scanf("%d%d%d\n", &op, &x, &y);
        if (op == 1)
        {
            x1 = tata(x);
            y1 = tata(y);
            t[x1] = y1;
        }
        else
        {
            x1 = tata(x);
            y1 = tata(y);
            if (x1 == y1) printf("DA\n");
            else printf("NU\n");
        }
    }
    return 0;
}