Cod sursa(job #3192229)

Utilizator Vlad_NistorNIstor Vlad Vlad_Nistor Data 11 ianuarie 2024 20:35:10
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <bits/stdc++.h>
using namespace std;
#define NMAX 100001

int tatic[NMAX];

int getRoot(int node){
    while(tatic[node] != node){
        node = tatic[node];
    }
    return node;
}
void uniteTrees(int left, int right){
    int x = getRoot(left), y = getRoot(right);
    if(x != y){
        tatic[x] = y;
    }
}

int main(void){
    ofstream cout("disjoint.out");
    ifstream cin("disjoint.in");
    int n, Q;
    cin >> n >> Q;
    for(int i = 1;i<=n;i++)tatic[i] = i;
    while(Q--){
        int x,y,z;
        cin >> x >> y >> z;
        if(x == 1){
            /// we need to unite y, z
            uniteTrees(y,z);
        }else{
            int root1 = getRoot(y), root2 = getRoot(z);
            if(root1 == root2)cout << "DA" << '\n'; /// apartine aceluiasi arbore avand acelasi tatic
            else cout << "NU" << '\n'; /// e negru
        }
    }
}