Cod sursa(job #3030166)

Utilizator samyro14Samy Dragos samyro14 Data 17 martie 2023 15:43:19
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>
using namespace std;

#define ll unsigned long long
#define fast_read ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define INF 0x3f3f3f3f

const int maxn = 2e5;
const int maxm = 4e5;
const int mod = 666013;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int n, m, cnt;
bitset<maxm + 1> vis;
int t[maxn + 2], rang[maxn + 2];
int ans;
struct edge{
    int x, y, cost;
} a[maxn + 2];
void read(){
    fin >> n >> m;
}

int get_root(int i){
    if(t[i] == i)
        return i;
    return t[i] = get_root(t[i]);
}
void unite(int r1, int r2){
    if(rang[r1] > rang[r2])
        t[r2] = r1;
    else{
        t[r1] = r2;
        if(rang[r1] == rang[r2])
            rang[r1] ++;
    }
}
void solve(){
    for(int i = 1; i <= n; ++i) rang[i] = 1, t[i] = i;
    for(int i = 1; i <= m; ++i){
        int task, x,  y; fin >> task >> x >> y;
        if(task == 1){
            unite(get_root(x), get_root(y));
        }
        else{
            int r1 = get_root(x);
            int r2 = get_root(y);
            if(r1 == r2)
                fout << "DA";
            else fout << "NU";
            fout << "\n";
        }
    }
}
int main(){
    read();
    solve();
    return 0;
}
/*


 */