Cod sursa(job #2142336)

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

using namespace std;

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

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;

    fin >> n >> m;

    while(m--)
    {
        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";
        }
    }
}