Cod sursa(job #2328684)

Utilizator VictorasulVictor Bertalan Victorasul Data 26 ianuarie 2019 09:42:20
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in") ;
ofstream g ("disjoint.out") ;
int N , x ,  y , M , c , cer , t[100005] , h[100005];
int Find (int x)
{
        int r = x ;
        while (t[r]) r = t[r];
        int y = x;
        int t1 ;
        while (y != x)
        {
            swap(t[y] , r) ;
        }
        return r;
}
void Union (int x  , int y)
{
        x = Find(x) ;
        y = Find(y) ;
        if (h[x] < h[y])
        {
            t[x] = y;
            c = y;
        }
        else
        {
            t[y] = x;
            c = x;
        }
        if (h[x] == h[y]) h[c] ++ ;
}
int main()
{
    f >> N >> M ;
    for (int i = 1 ; i <= M ; ++i)
    {
        f >> cer;
        f >> x >> y;
        if (cer == 1)
        {
            Union(x,y) ;
        }
        else
        {
            if (Find(x) == Find(y)) g << "DA" ;
            else g << "NU";

            g << '\n';
        }
    }
}