Cod sursa(job #2525963)

Utilizator smoc_georgemarianSmoc George-Marian smoc_georgemarian Data 18 ianuarie 2020 09:46:31
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

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


const int DMAX = 1e5+10;

int tata[DMAX];

int n,m;

void unire(int x,int y);
int comp(int x);

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t,i,op,x,y;

    fin>>n>>m;
    for(i=1;i<=m;i++){
        fin>>op>>x>>y;
        if(op & 1){
            if(comp(x) != comp(y))
                unire(x,y);
        }
        else{
            if(comp(x) == comp(y))
                fout<<"DA\n";
            else
                fout<<"NU\n";
        }
    }

    return 0;
}
int comp(int x){
    int root = x;
    while(tata[root])
        root = tata[root];

    int aux;
    while (tata[x]) {
        aux = tata[x];
        tata[x] = root;
        x = aux;
    }
    return root;
}
void unire(int x,int y){
    x=comp(x);
    y=comp(y);
    tata[x]=y;
}