Cod sursa(job #1745594)

Utilizator NicolaalexandraNicola Alexandra Mihaela Nicolaalexandra Data 22 august 2016 12:04:32
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>

using namespace std;
int n,m,i,rx,ry,t[100001],x,y,T;
int rad (int x){
    while (t[x] > 0)
        x = t[x];
    return x;
}
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");

int main (){

    fin>>n>>m;
    for (i=1;i<=n;i++)
        t[i] = -1;
    for (i=1;i<=m;i++){
        fin>>T>>x>>y;
        rx = rad (x);
        ry = rad (y);
        if (T == 2){
            if (rx == ry)
                fout<<"DA\n";
            else
                fout<<"NU\n";
        }
        else{
            if (rx != ry){
                if (t[rx] < t[ry]){
                    t[rx] += t[ry];
                    t[ry] = rx;
                }
                else{
                    t[ry] += t[rx];
                    t[rx] = ry;
                }
            }

        }
    }







    return 0;
}