Cod sursa(job #2587521)

Utilizator ParutixLungeanu Razvan Parutix Data 22 martie 2020 20:35:22
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;

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

int t[100001];

void Union(int x, int y)
{
    t[y] = x;
}

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;
}

int n, m, x, y, p, q, op;

int main()
{
    fin >> n >> m;
    for(int i = 1 ; i <= m ; ++i)
    {
        fin >> op >> x >> y;
        if(op == 2)
        {
            if(Find(x) == Find(y)) fout << "DA\n";
            else fout << "NU\n";
        }
        else
        {
            if(Find(x) == Find(y)) return 0;
            Union(Find(x), Find(y));
        }
    }
    return 0;
}