Cod sursa(job #3190687)

Utilizator MrPuzzleDespa Fabian Stefan MrPuzzle Data 7 ianuarie 2024 20:22:14
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>

#define DIM  100000

//#define int long long

using namespace std;

//ifstream f("in.in");
//ofstream g("out.out");

ifstream f("disjoint.in");
ofstream g("disjoint.out");

int n,m;
int c,x,y;
int len[DIM+5];
int t[DIM+5];

int getRoot(int x){

    if(t[x] == x){
        return x;
    }

    return getRoot(t[x]);

}

void join(int x,int y){

    x = getRoot(x);
    y = getRoot(y);

    if(len[x] > len[y]){
        swap(x,y);
    }

    t[x] = y;
    len[y] += len[x];
}

signed main(){

    f>>n>>m;

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

    for(int i=1;i<=m;i++){
        f>>c>>x>>y;
        if(c==1){
            join(x,y);
        }else{
            if(getRoot(x) == getRoot(y)){
                g<<"DA\n";
            }else{
                g<<"NU\n";
            }
        }
    }

    return 0;
}