Cod sursa(job #2861968)

Utilizator CiuiGinjoveanu Dragos Ciui Data 4 martie 2022 18:52:20
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;

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

const int MAX_SIZE = 100005;
int representants[MAX_SIZE];

int findRoot(int peak) {
    int root = representants[peak];
    while (root != representants[root]) {
        root = representants[root];
    }
    //cout << root << " ";
    return root;
}

void unite(int start, int end) {
    representants[start] = representants[end];
}

int main() {
    int peaks, arches;
    fin >> peaks >> arches;
    for (int i = 1; i <= peaks; ++i) {
        representants[i] = i;
    }
    for (int i = 1; i <= arches; ++i) {
        int operation, start, end;
        fin >> operation >> start >> end;
        if (operation == 1) {
            unite(findRoot(start), findRoot(end));
        } else {
            if (findRoot(start) == findRoot(end)) {
                fout << "DA";
            } else {
                fout << "NU";
            }
            fout << "\n";
        }
    }
    return 0;
}
/*
4 5
 1 1 2
 1 3 4
 1 1 3
 2 2 4
 2 1 4
 
 */