Cod sursa(job #2042739)

Utilizator vladdy47Bucur Vlad Andrei vladdy47 Data 18 octombrie 2017 23:18:31
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
# include <bits/stdc++.h>

using namespace std;

const int Nmax = 1e5 + 5;
int n, m, i, type, x, y, t[Nmax];

int Find(int x)
{
    if (t[x] != x) t[x] = Find(t[x]);
    return 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", &type, &x, &y);

        if (type == 1) t[x] = y;

        if (type == 2)
        {
            if (Find(x) == Find(y)) printf("DA\n");
            else printf("NU\n");
        }
    }

    return 0;
}