Cod sursa(job #2366861)

Utilizator qwerty1234qwerty qwerty1234 Data 4 martie 2019 22:31:24
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb

#include <bits/stdc++.h>


using namespace std;

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

const int Nmax = 1e5 + 5;

int t[Nmax] , n , m;

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


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

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