-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_components.cpp
57 lines (50 loc) · 1.21 KB
/
count_components.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <vector>
#include <sstream>
int find(std::vector<int>& roots, int id) {
while (roots[id] != id) {
roots[id] = roots[roots[id]];
id = roots[id];
}
return id;
}
int countComponents(int n, const std::vector<std::vector<int>>& edges) {
std::vector<int> roots(n);
for (int i = 0; i < n; i++) {
roots[i] = i;
}
for (const auto& e : edges) {
int root1 = find(roots, e[0]);
int root2 = find(roots, e[1]);
if (root1 != root2) {
roots[root1] = root2;
n--;
}
}
return n;
}
std::vector<std::vector<int>> input2DArrayInt() {
std::string text;
std::vector<std::vector<int>> twoDim;
while (std::getline(std::cin, text)) {
if (text.empty()) {
break;
}
std::istringstream iss(text);
std::vector<int> array;
int value;
while (iss >> value) {
array.push_back(value);
}
twoDim.push_back(array);
}
return twoDim;
}
int main() {
int n;
std::cin >> n;
std::cin.ignore();
std::vector<std::vector<int>> edges = input2DArrayInt();
std::cout << countComponents(n, edges);
return 0;
}