Cod sursa(job #2291948)

Utilizator dragos99Homner Dragos dragos99 Data 28 noiembrie 2018 20:14:59
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include<fstream>
using namespace std;
    ifstream f("disjoint.in");
    ofstream g("disjoint.out");
long n, m;

int main()
{
int cerinta, x, y;
f>>n>>m;

int lista[n];

for(int i = 1 ; i <= n ; i++){
    lista[i] = i;
}

for(int i = 1 ; i <= m ; i++){
    f>>cerinta>>x>>y;
    if(cerinta == 1){
        if(x > y)
            swap(x, y);
        /*while(lista[x] != x)
            x = lista[x];
        while(lista[y] != y)
            y = lista[y];*/
        lista[y] = x;
    }
    if(cerinta == 2){
        while(lista[x] != x)
            x = lista[x];
        while(lista[y] != y)
            y = lista[y];
        if(lista[x] == lista[y])
            g<<"DA"<<'\n';
        else g<<"NU"<<'\n';
    }
}

return 0;
}