Cod sursa(job #2145150)

Utilizator tanasaradutanasaradu tanasaradu Data 27 februarie 2018 09:59:35
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int Nmax = 100005;
int t[Nmax] , n , m;
inline void Union(int x , int y)
{
    t[y] = x;
}
inline int Find(int x)
{
    int rad , y;
    rad = x;
    while(t[rad])
        rad = t[rad];
    while(x != rad)
    {
        y = t[x];
        t[x] = rad; /// comprimarea drumurilor
        x = y;
    }
    return x;
}
int main()
{
    int x , y , op;
    fin >> n >> m;
    while(m -- )
    {
        fin >> op >> x >> y;
        x = Find(x);
        y = Find(y);
        if(op == 1)
        {
            if(x != y)
                Union(x , y);
        }
        else
        {
            if(x == y)
                fout << "DA\n";
            else fout << "NU\n";
        }
    }
    fin.close();
    fout.close();
    return 0;
}