Cod sursa(job #2200601)

Utilizator PushkinPetolea Cosmin Pushkin Data 1 mai 2018 22:50:09
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 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;
}
void unite(int x, int n2)
{
    int y=tata(n2);
    while(t[x])
    {
        n2=t[x];
        t[x]=y;
        x=n2;
    }
    t[x]=y;
}
int main()
{
    int op, x, y;
    f>>m>>m;
    while(f>>op>>x>>y)
    {
        if(op==1)
            unite(x, y);
        else if(tata(x)==tata(y))
                g<<"DA\n";
             else
                g<<"NU\n";
    }
    return 0;
}