Cod sursa(job #2945297)

Utilizator GhiuzanuEdward Ghiuzan Ghiuzanu Data 23 noiembrie 2022 18:03:11
Problema Paduri de multimi disjuncte Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

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

vector<int> t;

int find(int x){
    while(x != t[x]){
        x = t[x];
    }
    return x;
}
void op1(int x, int y){
    int x1, y1;
    x1 = find(x);
    y1 = find(y);
    t[y1] = x1;
}
void op2(int x, int y){
    if (find(x) == find(y)){
        fout<<"DA"<<endl;
    }
    else
        fout<<"NU"<<endl;
}


int main() {
    int n, m, cod, x, y;
    fin>>n>>m;
    t.resize(n + 1);
    for (int i = 1; i < n + 1; ++i) {
        t[i] = i;
    }
    for (int i = 0; i < m; ++i) {
        fin>>cod>>x>>y;
        if (cod == 1) {
            op1(x,y);
        }
        else {
            op2(x,y);
        }
    }
    return 0;
}