#include <bits/stdc++.h>
using namespace std;
using int64 = int64_t;
constexpr int INF = (int) 1e9;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> d(n, vector<int>(n, INF));
for (int i = 0; i < n; ++i) d[i][i] = 0;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
assert(d[a][b] == INF);
d[a][b] = d[b][a] = 1;
}
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
vector<vector<bool>> can(n, vector<bool>(n, false));
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (i != j) can[i][j] = true;
vector<vector<int>> res;
mt19937 rng(57);
vector<double> scores = vector<double>(n);
scores[n - 1] = 1;
for (int i = n - 2; i >= 0; --i) scores[i] = scores[i + 1] + (n - i);
vector<int> c(n);
vector<bool> avail(n, true);
vector<tuple<double, int, int>> cands;
vector<int> order(n);
double BONUS = 5;
double BONUS2 = 3;
while (true) {
bool any = false;
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (can[i][j]) any = true;
if (!any) break;
iota(c.begin(), c.end(), 0);
avail.assign(n, true);
iota(order.begin(), order.end(), 0);
while (true) {
shuffle(order.begin(), order.end(), rng);
cands.clear();
for (int ii = 0; ii < n; ++ii) {
int i = order[ii];
if (!avail[i]) continue;
for (int jj = ii + 1; jj < n; ++jj) {
int j = order[jj];
if (!avail[j] || d[i][j] != 1) continue;
double score = 0;
if (can[i][j]) score += BONUS * scores[1];
for (int k = 0; k < n; ++k) if (k != i && k != j) {
if (can[i][k] && !can[j][k]) score -= scores[d[i][k]] - scores[d[j][k]];
if (can[j][k] && !can[i][k]) score -= scores[d[j][k]] - scores[d[i][k]];
}
cands.emplace_back(-score, i, j);
}
{
bool ok = true;
double score = 0;
int cnt_in = 0;
int cnt_out = 0;
for (int j1 = 0; j1 < n; ++j1) if (d[i][j1] == 1) {
if (!avail[j1]) { ok = false; break; }
if (can[i][j1]) ++cnt_in; else ++cnt_out;
for (int j2 = j1 + 1; j2 < n; ++j2) if (d[i][j2] == 1) {
if (d[j1][j2] == 1) { ok = false; break; }
if (can[j1][j2]) score += BONUS2 * scores[1];
}
}
if (!ok) continue;
if (cnt_in) score -= BONUS2 * scores[1] * cnt_out;
for (int k = 0; k < n; ++k) if (d[i][k] >= 2) {
cnt_in = 0;
cnt_out = 0;
for (int j1 = 0; j1 < n; ++j1) if (d[i][j1] == 1) {
if (can[k][j1]) ++cnt_in; else ++cnt_out;
}
if (cnt_in) {
score += BONUS2 * scores[1] * (cnt_in - 1);
}
if (can[k][i]) {
score -= BONUS2 * scores[1] * (cnt_out + cnt_in);
}
}
cands.emplace_back(-score, i, -1);
}
}
sort(cands.begin(), cands.end());
for (auto [bs, bi, bj]: cands) {
if (bj < 0) {
bool ok = avail[bi];
for (int j1 = 0; j1 < n; ++j1) if (d[bi][j1] == 1) {
if (!avail[j1]) ok = false;
}
if (!ok) continue;
//cerr << "Using spoke at " << bi << ", score = " << bs << "\n";
avail[bi] = false;
bool h = false;
for (int j1 = 0; j1 < n; ++j1) if (d[bi][j1] == 1) {
avail[j1] = false;
c[j1] = c[bi];
if (can[bi][j1]) h = true;
for (int j2 = j1 + 1; j2 < n; ++j2) if (d[bi][j2] == 1) {
can[j1][j2] = false;
can[j2][j1] = false;
}
}
if (h) {
for (int j1 = 0; j1 < n; ++j1) if (d[bi][j1] == 1) {
can[bi][j1] = true;
can[j1][bi] = true;
}
}
for (int k = 0; k < n; ++k) if (k != bi && d[bi][k] != 1) {
bool center = can[bi][k];
bool spoke = false;
for (int j1 = 0; j1 < n; ++j1) if (d[bi][j1] == 1 && can[j1][k]) spoke = true;
can[bi][k] = spoke;
can[k][bi] = spoke;
for (int j1 = 0; j1 < n; ++j1) if (d[bi][j1] == 1) {
can[j1][k] = center;
can[k][j1] = center;
}
}
} else {
if (avail[bi] && avail[bj]) {
//cerr << "Using edge " << bi << "-" << bj << ", score = " << bs << "\n";
if (uniform_int_distribution<int>(0, 10)(rng) == 0) continue;
c[bi] = c[bj];
avail[bi] = false;
avail[bj] = false;
can[bi][bj] = can[bj][bi] = false;
for (int k = 0; k < n; ++k) if (k != bi && k != bj) {
if (can[bi][k] && !can[bj][k]) {
can[bj][k] = true;
can[k][bj] = true;
can[bi][k] = false;
can[k][bi] = false;
} else if (can[bj][k] && !can[bi][k]) {
can[bj][k] = false;
can[k][bj] = false;
can[bi][k] = true;
can[k][bi] = true;
}
}
}
}
}
break;
}
res.push_back(c);
//for (int i = 0; i < n; ++i) {
// for (int j = 0; j < n; ++j) if (can[i][j]) cerr << '+'; else cerr << '-';
// cerr << '\n';
//}
//cerr << endl;
}
printf("%d\n", res.size());
for (const auto& v : res) {
for (auto x : v) printf("%d ", x);
printf("\n");
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:165:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wformat=]
165 | printf("%d\n", res.size());
| ~^ ~~~~~~~~~~
| | |
| int std::vector<std::vector<int> >::size_type {aka long unsigned int}
| %ld
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
464 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
9 ms |
348 KB |
Output is correct |
8 |
Correct |
19 ms |
344 KB |
Output is correct |
9 |
Correct |
258 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
9 ms |
492 KB |
Output is correct |
5 |
Correct |
104 ms |
524 KB |
Output is correct |
6 |
Correct |
101 ms |
592 KB |
Output is correct |
7 |
Correct |
22 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
9 ms |
492 KB |
Output is correct |
10 |
Correct |
104 ms |
524 KB |
Output is correct |
11 |
Correct |
101 ms |
592 KB |
Output is correct |
12 |
Correct |
22 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
516 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
11 ms |
348 KB |
Output is correct |
21 |
Correct |
103 ms |
596 KB |
Output is correct |
22 |
Correct |
104 ms |
592 KB |
Output is correct |
23 |
Correct |
23 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
456 KB |
Output is correct |
27 |
Correct |
100 ms |
520 KB |
Output is correct |
28 |
Correct |
134 ms |
592 KB |
Output is correct |
29 |
Correct |
198 ms |
592 KB |
Output is correct |
30 |
Correct |
5 ms |
348 KB |
Output is correct |
31 |
Correct |
300 ms |
1048 KB |
Output is correct |
32 |
Correct |
84 ms |
592 KB |
Output is correct |
33 |
Correct |
111 ms |
592 KB |
Output is correct |
34 |
Correct |
120 ms |
596 KB |
Output is correct |
35 |
Correct |
99 ms |
528 KB |
Output is correct |
36 |
Correct |
50 ms |
348 KB |
Output is correct |
37 |
Correct |
7 ms |
348 KB |
Output is correct |
38 |
Correct |
33 ms |
532 KB |
Output is correct |
39 |
Correct |
79 ms |
516 KB |
Output is correct |
40 |
Correct |
153 ms |
508 KB |
Output is correct |
41 |
Correct |
302 ms |
852 KB |
Output is correct |
42 |
Correct |
10 ms |
348 KB |
Output is correct |
43 |
Correct |
28 ms |
348 KB |
Output is correct |
44 |
Correct |
112 ms |
596 KB |
Output is correct |
45 |
Correct |
95 ms |
544 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
344 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
464 KB |
Output is correct |
11 |
Correct |
3 ms |
348 KB |
Output is correct |
12 |
Correct |
9 ms |
348 KB |
Output is correct |
13 |
Correct |
19 ms |
344 KB |
Output is correct |
14 |
Correct |
258 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
9 ms |
492 KB |
Output is correct |
19 |
Correct |
104 ms |
524 KB |
Output is correct |
20 |
Correct |
101 ms |
592 KB |
Output is correct |
21 |
Correct |
22 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
516 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
11 ms |
348 KB |
Output is correct |
30 |
Correct |
103 ms |
596 KB |
Output is correct |
31 |
Correct |
104 ms |
592 KB |
Output is correct |
32 |
Correct |
23 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
456 KB |
Output is correct |
36 |
Correct |
100 ms |
520 KB |
Output is correct |
37 |
Correct |
134 ms |
592 KB |
Output is correct |
38 |
Correct |
198 ms |
592 KB |
Output is correct |
39 |
Correct |
5 ms |
348 KB |
Output is correct |
40 |
Correct |
300 ms |
1048 KB |
Output is correct |
41 |
Correct |
84 ms |
592 KB |
Output is correct |
42 |
Correct |
111 ms |
592 KB |
Output is correct |
43 |
Correct |
120 ms |
596 KB |
Output is correct |
44 |
Correct |
99 ms |
528 KB |
Output is correct |
45 |
Correct |
50 ms |
348 KB |
Output is correct |
46 |
Correct |
7 ms |
348 KB |
Output is correct |
47 |
Correct |
33 ms |
532 KB |
Output is correct |
48 |
Correct |
79 ms |
516 KB |
Output is correct |
49 |
Correct |
153 ms |
508 KB |
Output is correct |
50 |
Correct |
302 ms |
852 KB |
Output is correct |
51 |
Correct |
10 ms |
348 KB |
Output is correct |
52 |
Correct |
28 ms |
348 KB |
Output is correct |
53 |
Correct |
112 ms |
596 KB |
Output is correct |
54 |
Correct |
95 ms |
544 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
0 ms |
348 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
1 ms |
356 KB |
Output is correct |
78 |
Correct |
2 ms |
348 KB |
Output is correct |
79 |
Correct |
2 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
352 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |
84 |
Correct |
3 ms |
348 KB |
Output is correct |
85 |
Correct |
9 ms |
348 KB |
Output is correct |
86 |
Correct |
19 ms |
540 KB |
Output is correct |
87 |
Correct |
261 ms |
600 KB |
Output is correct |
88 |
Correct |
0 ms |
348 KB |
Output is correct |
89 |
Correct |
0 ms |
348 KB |
Output is correct |
90 |
Correct |
9 ms |
348 KB |
Output is correct |
91 |
Correct |
103 ms |
548 KB |
Output is correct |
92 |
Correct |
102 ms |
532 KB |
Output is correct |
93 |
Correct |
22 ms |
344 KB |
Output is correct |
94 |
Correct |
0 ms |
348 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
84 ms |
556 KB |
Output is correct |
98 |
Correct |
136 ms |
536 KB |
Output is correct |
99 |
Correct |
199 ms |
596 KB |
Output is correct |
100 |
Correct |
4 ms |
348 KB |
Output is correct |
101 |
Correct |
288 ms |
780 KB |
Output is correct |
102 |
Correct |
86 ms |
532 KB |
Output is correct |
103 |
Correct |
107 ms |
516 KB |
Output is correct |
104 |
Correct |
118 ms |
596 KB |
Output is correct |
105 |
Correct |
101 ms |
516 KB |
Output is correct |
106 |
Correct |
59 ms |
344 KB |
Output is correct |
107 |
Correct |
6 ms |
452 KB |
Output is correct |
108 |
Correct |
33 ms |
520 KB |
Output is correct |
109 |
Correct |
79 ms |
596 KB |
Output is correct |
110 |
Correct |
161 ms |
532 KB |
Output is correct |
111 |
Correct |
295 ms |
848 KB |
Output is correct |
112 |
Correct |
8 ms |
348 KB |
Output is correct |
113 |
Correct |
24 ms |
344 KB |
Output is correct |
114 |
Correct |
112 ms |
528 KB |
Output is correct |
115 |
Correct |
99 ms |
772 KB |
Output is correct |
116 |
Correct |
0 ms |
348 KB |
Output is correct |
117 |
Correct |
0 ms |
348 KB |
Output is correct |
118 |
Correct |
0 ms |
348 KB |
Output is correct |
119 |
Correct |
0 ms |
348 KB |
Output is correct |
120 |
Correct |
0 ms |
348 KB |
Output is correct |
121 |
Correct |
0 ms |
348 KB |
Output is correct |
122 |
Correct |
0 ms |
348 KB |
Output is correct |
123 |
Correct |
0 ms |
348 KB |
Output is correct |
124 |
Correct |
0 ms |
348 KB |
Output is correct |
125 |
Correct |
0 ms |
348 KB |
Output is correct |
126 |
Correct |
0 ms |
348 KB |
Output is correct |
127 |
Correct |
0 ms |
348 KB |
Output is correct |
128 |
Correct |
0 ms |
348 KB |
Output is correct |
129 |
Correct |
0 ms |
348 KB |
Output is correct |
130 |
Correct |
0 ms |
348 KB |
Output is correct |
131 |
Correct |
0 ms |
348 KB |
Output is correct |
132 |
Correct |
0 ms |
348 KB |
Output is correct |
133 |
Correct |
0 ms |
352 KB |
Output is correct |
134 |
Correct |
0 ms |
352 KB |
Output is correct |
135 |
Correct |
0 ms |
348 KB |
Output is correct |
136 |
Correct |
0 ms |
348 KB |
Output is correct |
137 |
Correct |
0 ms |
348 KB |
Output is correct |
138 |
Correct |
0 ms |
456 KB |
Output is correct |
139 |
Correct |
0 ms |
348 KB |
Output is correct |
140 |
Correct |
181 ms |
592 KB |
Output is correct |
141 |
Correct |
0 ms |
348 KB |
Output is correct |
142 |
Correct |
258 ms |
752 KB |
Output is correct |
143 |
Correct |
269 ms |
604 KB |
Output is correct |
144 |
Correct |
272 ms |
744 KB |
Output is correct |
145 |
Correct |
256 ms |
636 KB |
Output is correct |
146 |
Correct |
178 ms |
564 KB |
Output is correct |
147 |
Correct |
122 ms |
528 KB |
Output is correct |
148 |
Correct |
253 ms |
604 KB |
Output is correct |
149 |
Correct |
361 ms |
852 KB |
Output is correct |
150 |
Correct |
197 ms |
580 KB |
Output is correct |
151 |
Correct |
138 ms |
568 KB |
Output is correct |
152 |
Correct |
105 ms |
600 KB |
Output is correct |
153 |
Correct |
154 ms |
532 KB |
Output is correct |
154 |
Correct |
6 ms |
600 KB |
Output is correct |
155 |
Partially correct |
511 ms |
2360 KB |
Partially correct |
156 |
Correct |
69 ms |
844 KB |
Output is correct |
157 |
Partially correct |
2481 ms |
3820 KB |
Partially correct |
158 |
Correct |
205 ms |
596 KB |
Output is correct |
159 |
Correct |
133 ms |
592 KB |
Output is correct |
160 |
Correct |
94 ms |
596 KB |
Output is correct |
161 |
Partially correct |
111 ms |
1108 KB |
Partially correct |
162 |
Correct |
0 ms |
348 KB |
Output is correct |
163 |
Correct |
0 ms |
452 KB |
Output is correct |
164 |
Correct |
0 ms |
348 KB |
Output is correct |
165 |
Correct |
0 ms |
348 KB |
Output is correct |