Cod sursa(job #2919135)

Utilizator BlueLuca888Girbovan Robert Luca BlueLuca888 Data 15 august 2022 21:13:24
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")

using namespace std;

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

string msg[2] = {"NU\n", "DA\n"};

const int MAX_N = 100005;
int n, q, type, x, y;
int tata[MAX_N];

static inline int root(int nod){
    int cpy, rad;

    rad = nod;
    while(tata[rad] != 0)
        rad = tata[rad];

    while(tata[nod] != 0){
        cpy = nod;
        nod = tata[nod];
        tata[cpy] = rad;
    }

    return rad;
}

static inline void join(int x, int y){
    int rx = root(x);
    int ry = root(y);
    if(rx != ry)
        tata[rx] = ry;
}

int main (){
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr), fout.tie(nullptr);

    fin>>n>>q;
    while(q--){
        fin>>type>>x>>y;
        if(type == 1) /// join
            join(x, y);
        else /// query
            fout<<msg[root(x) == root(y)];
    }
    return 0;
}