Cod sursa(job #3160661)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 24 octombrie 2023 20:03:22
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 1e5;
int tata[NMAX+1];

int sef(int x){
    if(x == tata[x])
        return x;
    return tata[x] = sef(tata[x]);
}

void uneste(int x, int y){
    tata[sef(x)] = sef(y);
}

int main()
{
    int n, q;
    f >> n >> q;
    for(int i=1; i<=n; i++)
        tata[i] = i;
    for(int i=1; i<=q; i++){
        int t, x, y;
        f >> t >> x >> y;
        if(t == 1)
            uneste(x, y);
        else{
            if(sef(x) == sef(y))
                g << "DA" << "\n";
            else g << "NU" << "\n";
        }
    }

    return 0;
}