# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
301060 |
2020-09-17T16:07:32 Z |
JPN20 |
Simurgh (IOI17_simurgh) |
C++17 |
|
443 ms |
14584 KB |
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
// Input
int N, M, Q;
int A[1 << 18], B[1 << 18];
int C[509][509];
int idxs[509][509];
int ret[509][509];
// DFS Tree
bool used[1 << 18];
bool istree[1 << 18];
int par[1 << 18];
int wei[1 << 18];
int cl[1 << 18], cr[1 << 18];
vector<int> ord;
vector<int> X[1 << 18];
// DFS Tree Base
int BASE = 0;
vector<int> R;
void dfs(int pos) {
used[pos] = true;
cl[pos] = ord.size();
ord.push_back(pos);
for (int i : X[pos]) {
if (used[i] == true) continue;
par[i] = pos;
istree[idxs[pos][i]] = true;
dfs(i);
}
cr[pos] = ord.size() - 1;
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
// INPUT
N = n;
M = u.size();
for (int i = 0; i < M; i++) A[i] = u[i];
for (int i = 0; i < M; i++) B[i] = v[i];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) idxs[i][j] = -1;
}
for (int i = 0; i < M; i++) idxs[A[i]][B[i]] = i;
for (int i = 0; i < M; i++) idxs[B[i]][A[i]] = i;
// DFSTR
for (int i = 0; i < M; i++) X[A[i]].push_back(B[i]);
for (int i = 0; i < M; i++) X[B[i]].push_back(A[i]);
dfs(0);
for (int i = 0; i < M; i++) {
if (istree[i] == true) R.push_back(i);
}
BASE = count_common_roads(R); Q++;
// PRTRH
for (int i = 0; i < N; i++) wei[i] = -1;
for (int i = 0; i < M; i++) {
if (cl[A[i]] > cl[B[i]]) swap(A[i], B[i]);
int cx = B[i];
while (cx != A[i]) { C[A[i]][B[i]] = cx; C[B[i]][A[i]] = cx; cx = par[cx]; }
}
// TRHNT
for (int i : ord) {
if (i == 0 || wei[i] != -1) continue;
int idx1 = -1;
for (int j = 0; j < M; j++) {
if (istree[j] == true) continue;
if (!(cl[i] <= cl[B[j]] && cl[B[j]] <= cr[i])) continue;
if (cl[par[i]] <= cl[A[j]]) continue;
idx1 = j;
}
if (idx1 != -1) {
int s0 = i, s1 = par[i], s2 = par[par[i]];
vector<int> r1, r2;
for (int j : R) { if (j != idxs[s0][s1]) r1.push_back(j); }
for (int j : R) { if (j != idxs[s1][s2]) r2.push_back(j); }
r1.push_back(idx1);
r2.push_back(idx1);
int p1 = count_common_roads(r1); Q++;
int p2 = count_common_roads(r2); Q++;
int eval = p2 - (BASE - wei[s1]);
wei[i] = BASE - (p1 - eval);
}
else {
int idx2 = -1, maxid = -1; vector<int> idx3;
for (int j = 0; j < M; j++) {
if (istree[j] == true) continue;
if (!(cl[i] <= cl[B[j]] && cl[B[j]] <= cr[i])) continue;
if (par[i] != A[j]) continue;
idx3.push_back(j);
if (maxid < cl[B[j]]) { maxid = cl[B[j]]; idx2 = j; }
}
if (idx2 != -1) {
int cx = B[idx2];
vector<pair<int, int>> vals;
while (true) {
vector<int> r;
for (int j : R) { if (j != idxs[cx][par[cx]]) r.push_back(j); }
r.push_back(idx2);
vals.push_back(make_pair(cx, BASE - count_common_roads(r))); Q++;
cx = par[cx];
if (cx == A[idx2]) break;
}
int maxv = 0, minv = 0;
for (int j = 0; j < (int)vals.size(); j++) maxv = max(maxv, vals[j].second);
for (int j = 0; j < (int)vals.size(); j++) minv = min(minv, vals[j].second);
for (int j = 0; j < (int)vals.size(); j++) wei[vals[j].first] = vals[j].second;
if (maxv == 0 && minv == -1) {
for (int j = 0; j < (int)vals.size(); j++) wei[vals[j].first] += 1;
}
if (maxv == 0 && minv == 0) {
bool flg = false;
for (int j : idx3) {
vector<int> r;
for (int k : R) { if (k != idxs[i][par[i]]) r.push_back(k); }
r.push_back(j);
int eval = BASE - count_common_roads(r); Q++;
if (eval == -1) flg = true;
}
if (flg == false) {
for (int j = 0; j < (int)vals.size(); j++) wei[vals[j].first] += 1;
}
}
}
else {
wei[i] = 1;
}
}
}
// PRFNL
for (int i = 1; i < N; i++) {
ret[par[i]][i] = wei[i];
ret[i][par[i]] = wei[i];
}
// FINAL
for (int t = 2; t < ord.size(); t++) {
int i = ord[t];
vector<int> vec;
for (int j = 0; j < M; j++) {
if (istree[j] == true) continue;
if (B[j] == i) vec.push_back(A[j]);
}
int cx = vec.size(), cnt = 0;
while (cx >= 1) {
if (true) {
vector<int> r;
vector<bool> uses(N, false);
int ExpCost = BASE + cnt;
for (int k = 0; k < (int)vec.size(); k++) r.push_back(idxs[vec[k]][i]);
for (int k = 0; k < (int)vec.size(); k++) uses[C[vec[k]][i]] = true;
for (int k = 1; k < N; k++) { if (uses[k] == true) ExpCost -= ret[k][par[k]]; }
for (int k = 1; k < N; k++) { if (uses[k] == false) r.push_back(idxs[k][par[k]]); }
int val = count_common_roads(r);
if (val <= ExpCost) break;
}
int cl = 0, cr = cx, cm, maxn = -1;
int pres = -1;
for (int j = 0; j < 11; j++) {
cm = (cl + cr) / 2;
vector<int> r;
vector<bool> uses(N, false);
int ExpCost = BASE + cnt;
for (int k = cm; k < (int)vec.size(); k++) r.push_back(idxs[vec[k]][i]);
for (int k = cm; k < (int)vec.size(); k++) uses[C[vec[k]][i]] = true;
for (int k = 1; k < N; k++) { if (uses[k] == true) ExpCost -= ret[k][par[k]]; }
for (int k = 1; k < N; k++) { if (uses[k] == false) r.push_back(idxs[k][par[k]]); }
int val = count_common_roads(r);
if (val > ExpCost) { maxn = max(maxn, cm); cl = cm; }
else { cr = cm; }
if (pres == cm) break;
pres = cm;
}
if (maxn == -1) break;
cnt += 1;
ret[i][vec[maxn]] = 1;
ret[vec[maxn]][i] = 1;
cx = maxn;
}
}
// FLARE
vector<int> FinalAns;
for (int i = 0; i < M; i++) {
if (ret[A[i]][B[i]] == 1) FinalAns.push_back(i);
}
return FinalAns;
}
Compilation message
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:143:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
143 | for (int t = 2; t < ord.size(); t++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6528 KB |
correct |
2 |
Correct |
5 ms |
6528 KB |
correct |
3 |
Correct |
5 ms |
6528 KB |
correct |
4 |
Correct |
5 ms |
6528 KB |
correct |
5 |
Correct |
5 ms |
6528 KB |
correct |
6 |
Correct |
5 ms |
6528 KB |
correct |
7 |
Correct |
5 ms |
6528 KB |
correct |
8 |
Correct |
5 ms |
6528 KB |
correct |
9 |
Correct |
5 ms |
6528 KB |
correct |
10 |
Correct |
6 ms |
6528 KB |
correct |
11 |
Correct |
5 ms |
6528 KB |
correct |
12 |
Correct |
5 ms |
6528 KB |
correct |
13 |
Correct |
7 ms |
6656 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6528 KB |
correct |
2 |
Correct |
5 ms |
6528 KB |
correct |
3 |
Correct |
5 ms |
6528 KB |
correct |
4 |
Correct |
5 ms |
6528 KB |
correct |
5 |
Correct |
5 ms |
6528 KB |
correct |
6 |
Correct |
5 ms |
6528 KB |
correct |
7 |
Correct |
5 ms |
6528 KB |
correct |
8 |
Correct |
5 ms |
6528 KB |
correct |
9 |
Correct |
5 ms |
6528 KB |
correct |
10 |
Correct |
6 ms |
6528 KB |
correct |
11 |
Correct |
5 ms |
6528 KB |
correct |
12 |
Correct |
5 ms |
6528 KB |
correct |
13 |
Correct |
7 ms |
6656 KB |
correct |
14 |
Correct |
7 ms |
6912 KB |
correct |
15 |
Correct |
7 ms |
6912 KB |
correct |
16 |
Correct |
7 ms |
6912 KB |
correct |
17 |
Correct |
6 ms |
6912 KB |
correct |
18 |
Correct |
6 ms |
6912 KB |
correct |
19 |
Correct |
7 ms |
6912 KB |
correct |
20 |
Correct |
7 ms |
6912 KB |
correct |
21 |
Correct |
6 ms |
6912 KB |
correct |
22 |
Correct |
6 ms |
6912 KB |
correct |
23 |
Correct |
6 ms |
6912 KB |
correct |
24 |
Correct |
6 ms |
6912 KB |
correct |
25 |
Correct |
5 ms |
6784 KB |
correct |
26 |
Correct |
6 ms |
6912 KB |
correct |
27 |
Correct |
6 ms |
6912 KB |
correct |
28 |
Correct |
6 ms |
6912 KB |
correct |
29 |
Correct |
5 ms |
6784 KB |
correct |
30 |
Correct |
6 ms |
6912 KB |
correct |
31 |
Correct |
6 ms |
6912 KB |
correct |
32 |
Correct |
6 ms |
6912 KB |
correct |
33 |
Correct |
6 ms |
6912 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6528 KB |
correct |
2 |
Correct |
5 ms |
6528 KB |
correct |
3 |
Correct |
5 ms |
6528 KB |
correct |
4 |
Correct |
5 ms |
6528 KB |
correct |
5 |
Correct |
5 ms |
6528 KB |
correct |
6 |
Correct |
5 ms |
6528 KB |
correct |
7 |
Correct |
5 ms |
6528 KB |
correct |
8 |
Correct |
5 ms |
6528 KB |
correct |
9 |
Correct |
5 ms |
6528 KB |
correct |
10 |
Correct |
6 ms |
6528 KB |
correct |
11 |
Correct |
5 ms |
6528 KB |
correct |
12 |
Correct |
5 ms |
6528 KB |
correct |
13 |
Correct |
7 ms |
6656 KB |
correct |
14 |
Correct |
7 ms |
6912 KB |
correct |
15 |
Correct |
7 ms |
6912 KB |
correct |
16 |
Correct |
7 ms |
6912 KB |
correct |
17 |
Correct |
6 ms |
6912 KB |
correct |
18 |
Correct |
6 ms |
6912 KB |
correct |
19 |
Correct |
7 ms |
6912 KB |
correct |
20 |
Correct |
7 ms |
6912 KB |
correct |
21 |
Correct |
6 ms |
6912 KB |
correct |
22 |
Correct |
6 ms |
6912 KB |
correct |
23 |
Correct |
6 ms |
6912 KB |
correct |
24 |
Correct |
6 ms |
6912 KB |
correct |
25 |
Correct |
5 ms |
6784 KB |
correct |
26 |
Correct |
6 ms |
6912 KB |
correct |
27 |
Correct |
6 ms |
6912 KB |
correct |
28 |
Correct |
6 ms |
6912 KB |
correct |
29 |
Correct |
5 ms |
6784 KB |
correct |
30 |
Correct |
6 ms |
6912 KB |
correct |
31 |
Correct |
6 ms |
6912 KB |
correct |
32 |
Correct |
6 ms |
6912 KB |
correct |
33 |
Correct |
6 ms |
6912 KB |
correct |
34 |
Correct |
61 ms |
8984 KB |
correct |
35 |
Correct |
59 ms |
8860 KB |
correct |
36 |
Correct |
51 ms |
8776 KB |
correct |
37 |
Correct |
17 ms |
8064 KB |
correct |
38 |
Correct |
60 ms |
8960 KB |
correct |
39 |
Correct |
56 ms |
8832 KB |
correct |
40 |
Correct |
48 ms |
8832 KB |
correct |
41 |
Correct |
59 ms |
8952 KB |
correct |
42 |
Correct |
58 ms |
8952 KB |
correct |
43 |
Correct |
32 ms |
8576 KB |
correct |
44 |
Correct |
36 ms |
8448 KB |
correct |
45 |
Correct |
32 ms |
8448 KB |
correct |
46 |
Correct |
27 ms |
8320 KB |
correct |
47 |
Correct |
21 ms |
8192 KB |
correct |
48 |
Correct |
10 ms |
7936 KB |
correct |
49 |
Correct |
16 ms |
8064 KB |
correct |
50 |
Correct |
28 ms |
8244 KB |
correct |
51 |
Correct |
39 ms |
8444 KB |
correct |
52 |
Correct |
38 ms |
8448 KB |
correct |
53 |
Correct |
39 ms |
8440 KB |
correct |
54 |
Correct |
37 ms |
8568 KB |
correct |
55 |
Correct |
34 ms |
8448 KB |
correct |
56 |
Correct |
32 ms |
8448 KB |
correct |
57 |
Correct |
35 ms |
8448 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6528 KB |
correct |
2 |
Correct |
5 ms |
6656 KB |
correct |
3 |
Correct |
204 ms |
11828 KB |
correct |
4 |
Correct |
334 ms |
13688 KB |
correct |
5 |
Correct |
351 ms |
13816 KB |
correct |
6 |
Correct |
364 ms |
13688 KB |
correct |
7 |
Correct |
373 ms |
13820 KB |
correct |
8 |
Correct |
328 ms |
13688 KB |
correct |
9 |
Correct |
344 ms |
13816 KB |
correct |
10 |
Correct |
387 ms |
13736 KB |
correct |
11 |
Correct |
347 ms |
13728 KB |
correct |
12 |
Correct |
338 ms |
13688 KB |
correct |
13 |
Correct |
5 ms |
6528 KB |
correct |
14 |
Correct |
360 ms |
13688 KB |
correct |
15 |
Correct |
351 ms |
13656 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6528 KB |
correct |
2 |
Correct |
5 ms |
6528 KB |
correct |
3 |
Correct |
5 ms |
6528 KB |
correct |
4 |
Correct |
5 ms |
6528 KB |
correct |
5 |
Correct |
5 ms |
6528 KB |
correct |
6 |
Correct |
5 ms |
6528 KB |
correct |
7 |
Correct |
5 ms |
6528 KB |
correct |
8 |
Correct |
5 ms |
6528 KB |
correct |
9 |
Correct |
5 ms |
6528 KB |
correct |
10 |
Correct |
6 ms |
6528 KB |
correct |
11 |
Correct |
5 ms |
6528 KB |
correct |
12 |
Correct |
5 ms |
6528 KB |
correct |
13 |
Correct |
7 ms |
6656 KB |
correct |
14 |
Correct |
7 ms |
6912 KB |
correct |
15 |
Correct |
7 ms |
6912 KB |
correct |
16 |
Correct |
7 ms |
6912 KB |
correct |
17 |
Correct |
6 ms |
6912 KB |
correct |
18 |
Correct |
6 ms |
6912 KB |
correct |
19 |
Correct |
7 ms |
6912 KB |
correct |
20 |
Correct |
7 ms |
6912 KB |
correct |
21 |
Correct |
6 ms |
6912 KB |
correct |
22 |
Correct |
6 ms |
6912 KB |
correct |
23 |
Correct |
6 ms |
6912 KB |
correct |
24 |
Correct |
6 ms |
6912 KB |
correct |
25 |
Correct |
5 ms |
6784 KB |
correct |
26 |
Correct |
6 ms |
6912 KB |
correct |
27 |
Correct |
6 ms |
6912 KB |
correct |
28 |
Correct |
6 ms |
6912 KB |
correct |
29 |
Correct |
5 ms |
6784 KB |
correct |
30 |
Correct |
6 ms |
6912 KB |
correct |
31 |
Correct |
6 ms |
6912 KB |
correct |
32 |
Correct |
6 ms |
6912 KB |
correct |
33 |
Correct |
6 ms |
6912 KB |
correct |
34 |
Correct |
61 ms |
8984 KB |
correct |
35 |
Correct |
59 ms |
8860 KB |
correct |
36 |
Correct |
51 ms |
8776 KB |
correct |
37 |
Correct |
17 ms |
8064 KB |
correct |
38 |
Correct |
60 ms |
8960 KB |
correct |
39 |
Correct |
56 ms |
8832 KB |
correct |
40 |
Correct |
48 ms |
8832 KB |
correct |
41 |
Correct |
59 ms |
8952 KB |
correct |
42 |
Correct |
58 ms |
8952 KB |
correct |
43 |
Correct |
32 ms |
8576 KB |
correct |
44 |
Correct |
36 ms |
8448 KB |
correct |
45 |
Correct |
32 ms |
8448 KB |
correct |
46 |
Correct |
27 ms |
8320 KB |
correct |
47 |
Correct |
21 ms |
8192 KB |
correct |
48 |
Correct |
10 ms |
7936 KB |
correct |
49 |
Correct |
16 ms |
8064 KB |
correct |
50 |
Correct |
28 ms |
8244 KB |
correct |
51 |
Correct |
39 ms |
8444 KB |
correct |
52 |
Correct |
38 ms |
8448 KB |
correct |
53 |
Correct |
39 ms |
8440 KB |
correct |
54 |
Correct |
37 ms |
8568 KB |
correct |
55 |
Correct |
34 ms |
8448 KB |
correct |
56 |
Correct |
32 ms |
8448 KB |
correct |
57 |
Correct |
35 ms |
8448 KB |
correct |
58 |
Correct |
5 ms |
6528 KB |
correct |
59 |
Correct |
5 ms |
6656 KB |
correct |
60 |
Correct |
204 ms |
11828 KB |
correct |
61 |
Correct |
334 ms |
13688 KB |
correct |
62 |
Correct |
351 ms |
13816 KB |
correct |
63 |
Correct |
364 ms |
13688 KB |
correct |
64 |
Correct |
373 ms |
13820 KB |
correct |
65 |
Correct |
328 ms |
13688 KB |
correct |
66 |
Correct |
344 ms |
13816 KB |
correct |
67 |
Correct |
387 ms |
13736 KB |
correct |
68 |
Correct |
347 ms |
13728 KB |
correct |
69 |
Correct |
338 ms |
13688 KB |
correct |
70 |
Correct |
5 ms |
6528 KB |
correct |
71 |
Correct |
360 ms |
13688 KB |
correct |
72 |
Correct |
351 ms |
13656 KB |
correct |
73 |
Correct |
5 ms |
6528 KB |
correct |
74 |
Correct |
364 ms |
13856 KB |
correct |
75 |
Correct |
443 ms |
14564 KB |
correct |
76 |
Correct |
144 ms |
11000 KB |
correct |
77 |
Correct |
344 ms |
14548 KB |
correct |
78 |
Correct |
341 ms |
14564 KB |
correct |
79 |
Correct |
343 ms |
14584 KB |
correct |
80 |
Correct |
340 ms |
14456 KB |
correct |
81 |
Correct |
344 ms |
14072 KB |
correct |
82 |
Correct |
342 ms |
14456 KB |
correct |
83 |
Correct |
222 ms |
12280 KB |
correct |
84 |
Correct |
179 ms |
12796 KB |
correct |
85 |
Correct |
162 ms |
12536 KB |
correct |
86 |
Correct |
128 ms |
11512 KB |
correct |
87 |
Correct |
107 ms |
11000 KB |
correct |
88 |
Correct |
92 ms |
10688 KB |
correct |
89 |
Correct |
87 ms |
10488 KB |
correct |
90 |
Correct |
81 ms |
10488 KB |
correct |
91 |
Correct |
43 ms |
9728 KB |
correct |
92 |
Correct |
26 ms |
9600 KB |
correct |
93 |
Correct |
269 ms |
12536 KB |
correct |
94 |
Correct |
185 ms |
11512 KB |
correct |
95 |
Correct |
192 ms |
11512 KB |
correct |
96 |
Correct |
108 ms |
10488 KB |
correct |
97 |
Correct |
145 ms |
10616 KB |
correct |
98 |
Correct |
168 ms |
11012 KB |
correct |
99 |
Correct |
152 ms |
10616 KB |
correct |
100 |
Correct |
64 ms |
9856 KB |
correct |
101 |
Correct |
33 ms |
9600 KB |
correct |
102 |
Correct |
169 ms |
12224 KB |
correct |
103 |
Correct |
175 ms |
12152 KB |
correct |
104 |
Correct |
174 ms |
12152 KB |
correct |
105 |
Correct |
169 ms |
12280 KB |
correct |