Cod sursa(job #2142351)

Utilizator inquisitorAnders inquisitor Data 24 februarie 2018 22:47:37
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <cstdio>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

//freopen("disjoint.in", "r", stdin);
//freopen("disjoint.out", "w", stdout);

int t[100001];

int root(int x)
{
    int y = x, next;

    while(t[y])
    {
        y = t[y];
    }

    while(t[x])
    {
        next = t[x];

        t[x] = y;

        x = next;
    }

    return y;
}

int main()
{
    int n, m, cod, x, y;

    //scanf("%d %d", &n, &m);

    fin >> n >> m;

    while(m--)
    {
        //scanf("%d %d %d", &cod, &x, &y);

        fin >> cod >> x >> y;

        if(cod & 1)
        {
            t[root(x)] = root(y);
        }
        else
        {
            if (root(x) ^ root(y)) fout << "NU\n";

            else fout << "DA\n";
        }
    }
}