-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathComponentsInAGraph.cpp
77 lines (59 loc) · 1.63 KB
/
ComponentsInAGraph.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <bits/stdc++.h>
using namespace std;
/*
* Complete the componentsInGraph function below.
*/
int findParent(vector<int> & parent, int n) {
if(parent[n] == -1) return n;
return findParent(parent, parent[n]);
}
vector<int> componentsInGraph(vector<vector<int>> gb) {
vector<int> parent(2*gb.size()+1, -1);
vector<int> setSize(2*gb.size()+1, 1);
for(int i = 0; i < gb.size(); ++i) {
int x = findParent(parent, gb[i][0]);
int y = findParent(parent, gb[i][1]);
if(x != y) {
parent[y] = x;
setSize[x] += setSize[y];
setSize[y] = 0;
}
}
vector<int> result(2);
result[0] = INT_MAX;
result[1] = 0;
for(auto & i : setSize) {
if(result[1] < i) {
result[1] = i;
}
if(result[0] > i && i > 1) {
result[0] = i;
}
}
return result;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<vector<int>> gb(n);
for (int gb_row_itr = 0; gb_row_itr < n; gb_row_itr++) {
gb[gb_row_itr].resize(2);
for (int gb_column_itr = 0; gb_column_itr < 2; gb_column_itr++) {
cin >> gb[gb_row_itr][gb_column_itr];
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
vector<int> SPACE = componentsInGraph(gb);
for (int SPACE_itr = 0; SPACE_itr < SPACE.size(); SPACE_itr++) {
fout << SPACE[SPACE_itr];
if (SPACE_itr != SPACE.size() - 1) {
fout << " ";
}
}
fout << "\n";
fout.close();
return 0;
}