Cod sursa(job #2947083)

Utilizator Balauta_AlbertBalauta Albert Balauta_Albert Data 25 noiembrie 2022 18:00:54
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <fstream>
#define N 1000001
using namespace std;

ifstream fin ("disjoint.in");
ofstream fout("disjoint.out");

int n, m, t[N];

void Union(int x, int y){
    t[x] = y;
}

int Root(int x){
    int tata, root = x;
    while (t[root] != 0){
        root = t[root];
    }
    while (x != root){
        tata = t[x];
        t[x] = root;
        x = tata;
    }

    return root;
}

int main(){

    fin >> n >> m;
    int x, y, task;
    for (int i = 0; i < m; ++i){
        fin >> task >> x >> y;
        x = Root(x);
        y = Root(y);
        if (task == 1){
            Union(x, y);
        }
        else{
            if (x == y)
                fout << "DA\n";
            else
                fout << "NU\n";
        }
    }

    return 0;
}