Cod sursa(job #2200606)

Utilizator PushkinPetolea Cosmin Pushkin Data 1 mai 2018 22:59:30
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int m, t[100002];
inline int tata(int x)
{
    while(t[x])
        x=t[x];
    return x;
}
inline void compresie(int x, int r)
{
    int cx;
    while(t[x])
    {
        cx=t[x];
        t[x]=r;
        x=cx;
    }
}
int main()
{
    int op, x, y, r;
    f>>m>>m;
    while(f>>op>>x>>y)
    {
        if(op==1)
        {
            x=tata(x);
            y=tata(y);
            t[x]=y;
        }
        else
        {
            r=tata(x);
            compresie(x, r);
            if(r==tata(y))
                g<<"DA\n";
            else
                g<<"NU\n";
        }
    }
    return 0;
}