Cod sursa(job #2576141)

Utilizator andrei42Oandrei42O andrei42O Data 6 martie 2020 17:30:55
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m, T[N], getRoot(int);
int main()
{
    f >> n >> m;
    for(int i = 1; i <= n; i++)
        T[i] = i;
    for(; m; m--)
    {
        int c, x, y;
        f >> c >> x >> y;
        if(c == 1)
        {
            int dx = getRoot(x);
            int dy = getRoot(y);
            if(dx != dy)
                T[y] = dx;
        }
        else
            if(getRoot(x) == getRoot(y))
                g << "DA\n";
            else
                g << "NU\n";
    }
    return 0;
}
int getRoot(int nod)
{
   if(T[nod] == nod)
        return nod;
   T[nod] = getRoot(T[nod]);
   return T[nod];
}