# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
642052 |
2022-09-18T10:59:22 Z |
piOOE |
Simurgh (IOI17_simurgh) |
C++17 |
|
821 ms |
5344 KB |
#include <bits/stdc++.h>
#include "simurgh.h"
using namespace std;
void my_assert(bool f) {
if (!f) {
cerr << "228\n";
int x = 1337;
while (!f) {
x *= 2;
cerr << "yay";
}
}
}
struct dsu {
vector<int> p;
dsu(int n = 0) {
p.resize(n);
iota(p.begin(), p.end(), 0);
}
int get(int x) {
while (x != p[x]) x = p[x] = p[p[x]];
return x;
}
bool same(int x, int y) {
return get(x) == get(y);
}
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x == y) {
return false;
}
p[y] = x;
return true;
}
};
vector<int> find_roads(int n, std::vector<int> U, std::vector<int> V) {
if (U.size() != n * (n - 1) / 2) {
vector<pair<int, int>> par(n);
vector<int> tin(n), tout(n);
int T = 0, ans_st = 0;
auto calc = [&](vector<int> st) {
T = 0;
ans_st = count_common_roads(st);
vector<vector<pair<int, int>>> g(n);
for (int i = 0; i < n - 1; ++i) {
g[U[st[i]]].emplace_back(V[st[i]], i);
g[V[st[i]]].emplace_back(U[st[i]], i);
}
function<void(int)> dfs = [&](int v) {
tin[v] = T++;
for (auto [to, i]: g[v]) {
if (to != par[v].first) {
par[to] = {v, i};
dfs(to);
}
}
tout[v] = T;
};
dfs(0);
};
int m = U.size();
dsu d(n);
vector<int> st;
vector<bool> in_st(m);
for (int i = 0; i < m; ++i) {
in_st[i] = d.unite(U[i], V[i]);
if (in_st[i]) {
st.push_back(i);
}
}
auto isp = [&](int x, int y) {
return tin[x] <= tin[y] && tout[x] >= tout[y];
};
auto path = [&](int x, int y) -> vector<int> {
int y_ = y, x_ = x;
vector<int> ans;
while (!isp(x, y_)) {
ans.push_back(par[x].second);
x = par[x].first;
}
while (!isp(y, x_)) {
ans.push_back(par[y].second);
y = par[y].first;
}
return ans;
};
calc(st);
vector<bool> sure(m);
for (int i = 0; i < m; ++i) {
if (find(st.begin(), st.end(), i) == st.end()) {
vector<int> now = path(U[i], V[i]);
vector<int> vis;
bool yay = false;
for (int j: now) {
if (!sure[st[j]]) {
auto q = st;
q.erase(q.begin() + j);
q.push_back(i);
int val = count_common_roads(q);
if (val > ans_st) {
st = q;
sure[i] = true;
calc(st);
yay = true;
break;
} else if (val < ans_st) {
sure[st[j]] = true;
break;
} else {
vis.push_back(st[j]);
}
}
}
if (yay) {
for (int j: vis) {
sure[j] = true;
}
} else {
if (!vis.empty()) {
for (int j: vis) {
auto it = find(st.begin(), st.end(), j);
if (it != st.end()) {
st.erase(it);
}
}
dsu s(n);
for (int j: st) {
assert(s.unite(U[j], V[j]));
}
for (int j = i + 1; j < m; ++j) {
if (s.unite(U[j], V[j])) {
st.push_back(j);
}
}
calc(st);
}
}
}
}
return st;
} else {
vector a(n, vector<int>(n, -1)), id(n, vector<int>(n));
const int m = n * (n - 1) / 2;
for (int i = 0; i < m; ++i) {
id[U[i]][V[i]] = id[V[i]][U[i]] = i;
}
vector<int> cnt(n), left(n);
for (int i = 0; i < n; ++i) {
vector<int> q;
for (int j = 0; j < n; ++j) {
if (j != i) {
q.push_back(id[i][j]);
}
}
cnt[i] = count_common_roads(q);
}
left = cnt;
vector<int> ans;
vector<bool> used(n);
for (int iter = 0; iter < n; ++iter) {
int v = -1;
for (int i = 0; i < n; ++i) {
if (!used[i] && (v == -1 || left[v] > left[i])) {
v = i;
}
}
used[v] = true;
int chosen = -1;
if (left[v] != 0) {
assert(left[v] == 1);
vector<int> c;
for (int to = 0; to < n; ++to) {
if (to != v && a[v][to] == -1) {
c.push_back(to);
}
}
while (c.size() != 1) {
int wait = c.size() & 1 ? c.back() : -1;
if (c.size() & 1) {
c.pop_back();
}
int sz = c.size() >> 1;
vector<int> st;
vector<bool> in_c(n);
for (int x: c) {
in_c[x] = true;
}
for (int to = 0; to < n; ++to) {
if (to != v && !in_c[to]) {
st.push_back(id[v][to]);
}
}
for (int i = 0; i < sz; ++i) {
st.push_back(id[c[i]][c[i + sz]]);
}
vector<int> L(st);
for (int i = 0; i < sz; ++i) {
L.push_back(id[v][c[i]]);
}
vector<int> R(st);
for (int i = sz; i < sz * 2; ++i) {
R.push_back(id[v][c[i]]);
}
int cntL = count_common_roads(L), cntR = count_common_roads(R);
if (cntL == cntR) {
assert(wait != -1);
c.clear();
} else if (cntL < cntR) {
c.erase(c.begin(), c.begin() + sz);
} else {
c.resize(sz);
}
if (wait != -1) {
c.push_back(wait);
}
}
assert(!c.empty());
chosen = c[0];
ans.push_back(id[v][chosen]);
}
for (int to = 0; to < n; ++to) {
if (to != v && a[v][to] == -1) {
if (to != chosen) {
a[v][to] = a[to][v] = 0;
} else {
a[v][to] = a[to][v] = 1;
--left[to];
}
}
}
}
return ans;
}
}
Compilation message
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:46:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
46 | if (U.size() != n * (n - 1) / 2) {
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
300 KB |
correct |
3 |
Correct |
1 ms |
212 KB |
correct |
4 |
Correct |
1 ms |
212 KB |
correct |
5 |
Correct |
1 ms |
212 KB |
correct |
6 |
Correct |
0 ms |
300 KB |
correct |
7 |
Correct |
1 ms |
300 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
300 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
212 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
1 ms |
212 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
300 KB |
correct |
3 |
Correct |
1 ms |
212 KB |
correct |
4 |
Correct |
1 ms |
212 KB |
correct |
5 |
Correct |
1 ms |
212 KB |
correct |
6 |
Correct |
0 ms |
300 KB |
correct |
7 |
Correct |
1 ms |
300 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
300 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
212 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
1 ms |
212 KB |
correct |
14 |
Correct |
1 ms |
340 KB |
correct |
15 |
Correct |
1 ms |
340 KB |
correct |
16 |
Correct |
1 ms |
340 KB |
correct |
17 |
Correct |
4 ms |
212 KB |
correct |
18 |
Correct |
2 ms |
212 KB |
correct |
19 |
Correct |
5 ms |
328 KB |
correct |
20 |
Correct |
4 ms |
212 KB |
correct |
21 |
Correct |
4 ms |
212 KB |
correct |
22 |
Correct |
1 ms |
212 KB |
correct |
23 |
Correct |
1 ms |
300 KB |
correct |
24 |
Correct |
1 ms |
296 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
296 KB |
correct |
27 |
Correct |
1 ms |
212 KB |
correct |
28 |
Correct |
1 ms |
212 KB |
correct |
29 |
Correct |
1 ms |
212 KB |
correct |
30 |
Correct |
1 ms |
212 KB |
correct |
31 |
Correct |
1 ms |
212 KB |
correct |
32 |
Correct |
1 ms |
212 KB |
correct |
33 |
Correct |
1 ms |
300 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
300 KB |
correct |
3 |
Correct |
1 ms |
212 KB |
correct |
4 |
Correct |
1 ms |
212 KB |
correct |
5 |
Correct |
1 ms |
212 KB |
correct |
6 |
Correct |
0 ms |
300 KB |
correct |
7 |
Correct |
1 ms |
300 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
300 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
212 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
1 ms |
212 KB |
correct |
14 |
Correct |
1 ms |
340 KB |
correct |
15 |
Correct |
1 ms |
340 KB |
correct |
16 |
Correct |
1 ms |
340 KB |
correct |
17 |
Correct |
4 ms |
212 KB |
correct |
18 |
Correct |
2 ms |
212 KB |
correct |
19 |
Correct |
5 ms |
328 KB |
correct |
20 |
Correct |
4 ms |
212 KB |
correct |
21 |
Correct |
4 ms |
212 KB |
correct |
22 |
Correct |
1 ms |
212 KB |
correct |
23 |
Correct |
1 ms |
300 KB |
correct |
24 |
Correct |
1 ms |
296 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
296 KB |
correct |
27 |
Correct |
1 ms |
212 KB |
correct |
28 |
Correct |
1 ms |
212 KB |
correct |
29 |
Correct |
1 ms |
212 KB |
correct |
30 |
Correct |
1 ms |
212 KB |
correct |
31 |
Correct |
1 ms |
212 KB |
correct |
32 |
Correct |
1 ms |
212 KB |
correct |
33 |
Correct |
1 ms |
300 KB |
correct |
34 |
Correct |
27 ms |
1332 KB |
correct |
35 |
Correct |
673 ms |
940 KB |
correct |
36 |
Correct |
387 ms |
752 KB |
correct |
37 |
Correct |
16 ms |
340 KB |
correct |
38 |
Correct |
683 ms |
1088 KB |
correct |
39 |
Correct |
616 ms |
872 KB |
correct |
40 |
Correct |
415 ms |
744 KB |
correct |
41 |
Correct |
26 ms |
1436 KB |
correct |
42 |
Correct |
821 ms |
980 KB |
correct |
43 |
Correct |
9 ms |
596 KB |
correct |
44 |
Correct |
8 ms |
596 KB |
correct |
45 |
Correct |
8 ms |
644 KB |
correct |
46 |
Correct |
7 ms |
468 KB |
correct |
47 |
Correct |
4 ms |
340 KB |
correct |
48 |
Correct |
1 ms |
212 KB |
correct |
49 |
Correct |
2 ms |
340 KB |
correct |
50 |
Correct |
4 ms |
340 KB |
correct |
51 |
Correct |
8 ms |
644 KB |
correct |
52 |
Correct |
9 ms |
604 KB |
correct |
53 |
Correct |
7 ms |
468 KB |
correct |
54 |
Correct |
9 ms |
724 KB |
correct |
55 |
Correct |
9 ms |
596 KB |
correct |
56 |
Correct |
9 ms |
596 KB |
correct |
57 |
Correct |
10 ms |
660 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
0 ms |
212 KB |
correct |
3 |
Correct |
84 ms |
3388 KB |
correct |
4 |
Correct |
138 ms |
5300 KB |
correct |
5 |
Correct |
142 ms |
5200 KB |
correct |
6 |
Correct |
134 ms |
5216 KB |
correct |
7 |
Correct |
137 ms |
5196 KB |
correct |
8 |
Correct |
135 ms |
5104 KB |
correct |
9 |
Correct |
138 ms |
5220 KB |
correct |
10 |
Correct |
136 ms |
5216 KB |
correct |
11 |
Correct |
136 ms |
5104 KB |
correct |
12 |
Correct |
136 ms |
5196 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
135 ms |
5216 KB |
correct |
15 |
Correct |
140 ms |
5344 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
300 KB |
correct |
3 |
Correct |
1 ms |
212 KB |
correct |
4 |
Correct |
1 ms |
212 KB |
correct |
5 |
Correct |
1 ms |
212 KB |
correct |
6 |
Correct |
0 ms |
300 KB |
correct |
7 |
Correct |
1 ms |
300 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
300 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
212 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
1 ms |
212 KB |
correct |
14 |
Correct |
1 ms |
340 KB |
correct |
15 |
Correct |
1 ms |
340 KB |
correct |
16 |
Correct |
1 ms |
340 KB |
correct |
17 |
Correct |
4 ms |
212 KB |
correct |
18 |
Correct |
2 ms |
212 KB |
correct |
19 |
Correct |
5 ms |
328 KB |
correct |
20 |
Correct |
4 ms |
212 KB |
correct |
21 |
Correct |
4 ms |
212 KB |
correct |
22 |
Correct |
1 ms |
212 KB |
correct |
23 |
Correct |
1 ms |
300 KB |
correct |
24 |
Correct |
1 ms |
296 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
296 KB |
correct |
27 |
Correct |
1 ms |
212 KB |
correct |
28 |
Correct |
1 ms |
212 KB |
correct |
29 |
Correct |
1 ms |
212 KB |
correct |
30 |
Correct |
1 ms |
212 KB |
correct |
31 |
Correct |
1 ms |
212 KB |
correct |
32 |
Correct |
1 ms |
212 KB |
correct |
33 |
Correct |
1 ms |
300 KB |
correct |
34 |
Correct |
27 ms |
1332 KB |
correct |
35 |
Correct |
673 ms |
940 KB |
correct |
36 |
Correct |
387 ms |
752 KB |
correct |
37 |
Correct |
16 ms |
340 KB |
correct |
38 |
Correct |
683 ms |
1088 KB |
correct |
39 |
Correct |
616 ms |
872 KB |
correct |
40 |
Correct |
415 ms |
744 KB |
correct |
41 |
Correct |
26 ms |
1436 KB |
correct |
42 |
Correct |
821 ms |
980 KB |
correct |
43 |
Correct |
9 ms |
596 KB |
correct |
44 |
Correct |
8 ms |
596 KB |
correct |
45 |
Correct |
8 ms |
644 KB |
correct |
46 |
Correct |
7 ms |
468 KB |
correct |
47 |
Correct |
4 ms |
340 KB |
correct |
48 |
Correct |
1 ms |
212 KB |
correct |
49 |
Correct |
2 ms |
340 KB |
correct |
50 |
Correct |
4 ms |
340 KB |
correct |
51 |
Correct |
8 ms |
644 KB |
correct |
52 |
Correct |
9 ms |
604 KB |
correct |
53 |
Correct |
7 ms |
468 KB |
correct |
54 |
Correct |
9 ms |
724 KB |
correct |
55 |
Correct |
9 ms |
596 KB |
correct |
56 |
Correct |
9 ms |
596 KB |
correct |
57 |
Correct |
10 ms |
660 KB |
correct |
58 |
Correct |
1 ms |
212 KB |
correct |
59 |
Correct |
0 ms |
212 KB |
correct |
60 |
Correct |
84 ms |
3388 KB |
correct |
61 |
Correct |
138 ms |
5300 KB |
correct |
62 |
Correct |
142 ms |
5200 KB |
correct |
63 |
Correct |
134 ms |
5216 KB |
correct |
64 |
Correct |
137 ms |
5196 KB |
correct |
65 |
Correct |
135 ms |
5104 KB |
correct |
66 |
Correct |
138 ms |
5220 KB |
correct |
67 |
Correct |
136 ms |
5216 KB |
correct |
68 |
Correct |
136 ms |
5104 KB |
correct |
69 |
Correct |
136 ms |
5196 KB |
correct |
70 |
Correct |
0 ms |
212 KB |
correct |
71 |
Correct |
135 ms |
5216 KB |
correct |
72 |
Correct |
140 ms |
5344 KB |
correct |
73 |
Correct |
0 ms |
212 KB |
correct |
74 |
Incorrect |
139 ms |
5212 KB |
WA in grader: NO |
75 |
Halted |
0 ms |
0 KB |
- |