Cod sursa(job #2859466)

Utilizator CiuiGinjoveanu Dragos Ciui Data 1 martie 2022 14:05:43
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <algorithm>
#include <iostream>
#include <cstring>
#include <fstream>
#include <cassert>
#include <vector>
#include <set>
#include <queue>
using namespace std;
 
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
 
const int MAX_SIZE = 100005;
 
int representants[MAX_SIZE];
 
int main() {
    int peaks, operations;
    fin >> peaks >> operations;
    for (int i = 1; i <= peaks; ++i) {
        representants[i] = i;
    }
    for (int i = 1; i <= operations; ++i) {
        int operation, start, end;
        fin >> operation >> start >> end;
        if (operation == 1) {
            representants[end] = representants[start];
        } else {
            if (representants[start] == representants[end]) {
                fout << "DA";
            } else {
                fout << "NU";
            }
            fout << "\n";
        }
    }
}
/*
 
 Ideea de rezolvare pentru Paduri de multimi disjuncte*):
 https://infoarena.ro/problema/disjoint
 
Prima oara m-am gandit sa fac conexiunile intre puncte, iar apoi sa ma folosesc de BFS pentru a vedea daca pot ajunge de la punctul x la y si astfel sa afisez da sau nu si am primit 40pct si TLE.
 
 Apoi:

 

 1 1
 2 1 1 -> DA
 
 5 6
 1 2 1
 2 2 1
 2 1 2
 2 1 3
 2 2 3
 2 3 5 -> DA DA NU NU NU, bun
 
 6 14
 1 1 6
 1 4 5
 1 4 2
 1 6 2
 1 2 3
 2 1 2
 2 1 3
 2 1 4
 2 1 5
 2 1 6
 2 2 6
 2 2 1
 2 3 5
 2 3 4 -> 9X DA -> bun
 
 4 6
 1 1 2
 1 3 4
 2 1 3
 2 1 2
 1 1 3
 2 1 4 -> NU, DA, DA -> bun
 
 */