-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxComponent.cpp
119 lines (112 loc) · 2.49 KB
/
MaxComponent.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>;
#include <vector>;
using namespace std;
int n;
int m;
vector<vector<int>> g;
vector<int> used;
vector<int> comp;
vector<int> sizes;
vector<int> eagles;
vector<int> mini;
int cc = 0;
void dfs(int v, int key) {
used[v] = cc;
if (key == 0) {
for (auto to : g[v]) {
if (used[to] == -1) {
dfs(to, 0);
}
}
}
else{
mini[cc] = min(mini[cc], v);
sizes[cc]++;
used[v] = cc;
for (auto to : g[v]) {
eagles[cc]++;
if (used[to] == -1) {
dfs(to, 1);
}
}
}
}
int main() {
cin >> n;
cin >> m;
g.resize(n);
used.resize(n, -1);
for (int i = 0; i < m; i++) {
int v1, v2;
cin >> v1 >> v2;
g[v1].push_back(v2);
g[v2].push_back(v1);
}
for (int i = 0; i < n; ++i) {
if (used[i] == -1) {
dfs(i, 0);
cc++;
}
}
vector<vector<int>> cv(cc);
sizes.resize(cc, 0);
eagles.resize(cc, 0);
mini.resize(cc, 0);
used.resize(n, -1);
for (int i = 0; i < n; i++) {
used[i] = -1;
}
cc = 0;
for (int i = 0; i < n; ++i) {
if (used[i] == -1) {
dfs(i, 1);
cc++;
}
}
for (int i = 0; i < n; i++) {
cv[used[i]].push_back(i);
}
int best = 0;
for (int i = 0; i < cc; i++) {
if (sizes[i] > sizes[best]) {
best = i;
}
else {
if (sizes[i] == sizes[best]) {
if (eagles[i] > eagles[best]) {
best = i;
}
else {
if (eagles[i] == eagles[best]) {
if (mini[i] < mini[best]) {
best = i;
}
}
}
}
}
}
cout << "graph {" << endl;
for (int i = 0; i < n; i++) {
if (used[i] == best) {
cout << i << "[color = red]" << endl;
}
else {
cout << i << endl;
}
}
for (int i = 0; i < cc; i++) {
for (auto v : cv[i]) {
for (auto v2 : g[v]) {
if (i == best) {
cout << v << " -- " << v2 << "[color = red]" << endl;
}
else {
cout << v << " -- " << v2 << endl;
}
}
}
}
cout << "}";
return 0;
}