Pagini recente » Cod sursa (job #660195) | Cod sursa (job #2519424) | Cod sursa (job #725700) | Cod sursa (job #2584671) | Cod sursa (job #2696170)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
const int N_MAX = 100005;
const int INF = 1e9;
int group[N_MAX], father[N_MAX];
int n, m, k;
int find (int node) {
if (node == father[node])
return node;
int new_father = find(father[node]);
father[node] = new_father; /// optimizare
return new_father;
}
int main() {
std::ifstream fin("catun.in");
std::ofstream fout("catun.out");
fin >> n >> m;
for (int i = 1; i <= n; ++i) {
father[i] = i;
group[i] = 1;
}
for (int i = 1; i <= m; ++i) {
int tip, a, b;
fin >> tip >> a >> b;
int father_a = find(a);
int father_b = find(b);
if (tip == 1) {
if (father_a != father_b) {
if (group[father_a] > group[father_b]) {
father[father_b] = father_a;
group[father_a] += group[father_b]; ///optimizare
} else {
father[father_a] = father_b;
group[father_b] += group[father_a]; ///optimizare
}
}
} else {
if (father_a == father_b)
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}