Pagini recente » Cod sursa (job #1727699) | Cod sursa (job #3287635) | Cod sursa (job #109799) | Cod sursa (job #814652) | Cod sursa (job #2859466)
#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
*/