Cod sursa(job #2568819)

Utilizator ikogamesIon Ceaun ikogames Data 4 martie 2020 10:02:39
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <bits/stdc++.h>

using namespace std;

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

int t[100001], n;

void Union(int x, int y)
{
    t[x] = y;
}
int Find(int x)
{
    int rad = x, y;
    while(t[rad] != 0)
        rad = t[rad];
    while(x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}

void Read()
{
    int x, y, op;
    fin >> n >> n;
    for(int i = 1; i <= n; i++)
    {
        fin >> op >> x >> y;
        x = Find(x);
        y = Find(y);
        if(op == 1 && x != y) Union(x, y);
        else if(op == 2)
        {
            if(x != y) fout << "NU\n";
            else fout << "DA\n";
        }

    }
}

int main()
{
    Read();
    return 0;
}