Cod sursa(job #2426460)

Utilizator bluestorm57Vasile T bluestorm57 Data 28 mai 2019 08:52:33
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

const int NMAX = 100005;
int n,m,ne[NMAX],rang[NMAX];

int found(int x)
{
    if(x != ne[x])
        ne[x] = found(ne[x]);
    return ne[x];
}

void unite(int x, int y){
    if(rang[x] > rang[y])
        ne[y] = x;
    else
        ne[x] = y;

    if(rang[x] == rang[y])
        rang[y]++;
}

int main(){
    int i,j,cod,x,y;
    f >> n >> m;
    for(i = 1 ; i <= n ; i++){
        ne[i] = i;
        rang[i] = 1;
    }
    for(i = 1 ; i <= m ; i++){
        f >> cod >> x >> y;
        //g << found(x) << " " << found(y) << "\n";
        if(cod == 2){
            if(found(x) == found(y))
                g << "DA\n";
            else
                g << "NU\n";
        }else
            if(found(x) != found(y))
                unite(found(x) , found(y));

    }

    return 0;
}