# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
578155 |
2022-06-16T06:21:23 Z |
Kanon |
Simurgh (IOI17_simurgh) |
C++14 |
|
1010 ms |
7816 KB |
#include <bits/stdc++.h>
#include "simurgh.h"
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__);
class dsu {
public:
vector<int> p;
int n;
dsu(int _n) : n(_n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
}
inline int get(int x) {
return (x == p[x] ? x : (p[x] = get(p[x])));
}
inline bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x != y) {
p[x] = y;
return true;
}
return false;
}
};
vector<int> find_roads(int n, vector<int> n1, vector<int> n2) {
int m = n1.size();
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
g[n1[i]].push_back(i);
g[n2[i]].push_back(i);
}
vector<int> par(n, -1);
vector<int> dep(n);
vector<int> was(n);
function<void(int, int)> dfs = [&](int v, int p) {
was[v] = 1;
par[v] = p;
for (int e : g[v]) {
if (e == p) {
continue;
}
int u = n1[e] ^ n2[e] ^ v;
if (was[u]) {
continue;
}
dep[u] = dep[v] + 1;
dfs(u, e);
}
};
dfs(0, -1);
set<int> tree_edges;
for (int i = 0; i < n; i++) {
if (par[i] != -1) {
tree_edges.insert(par[i]);
}
}
vector<int> ret(m, -1);
auto royal = [&](set<int> S) {
vector<int> p;
for (int i : S) {
p.push_back(i);
}
assert((int) p.size() == n - 1);
return count_common_roads(p);
};
{
auto handle_cycle = [&](vector<int> c) {
assert(c.size() >= 3);
set<int> S = tree_edges;
int e = -1, ve = -1;
for (int i : c) {
if (ret[i] != -1) {
e = i;
ve = ret[i];
}
S.insert(i);
}
assert((int) S.size() == n);
if (e != -1) {
S.erase(e);
int vS = royal(S) + ve;
S.insert(e);
for (int i : c) {
if (ret[i] != -1) {
continue;
}
S.erase(i);
assert(ret[i] == vS - royal(S) || ret[i] == -1);
ret[i] = vS - royal(S);
S.insert(i);
}
} else {
vector<int> val;
for (int i : c) {
S.erase(i);
val.push_back(royal(S));
S.insert(i);
}
int mx = *max_element(val.begin(), val.end());
for (int i = 0; i < (int) c.size(); i++) {
if (val[i] == mx) {
assert(ret[c[i]] != 1);
ret[c[i]] = 0;
} else {
assert(ret[c[i]] != 0);
ret[c[i]] = 1;
}
}
}
};
function<vector<int>(int)> calc = [&](int v) {
vector<int> cycle;
int highest = dep[v];
int back_ed = -1;
for (int e : g[v]) {
if (e == par[v]) {
continue;
}
int u = n1[e] ^ n2[e] ^ v;
if (dep[u] < highest) {
highest = dep[u];
back_ed = e;
}
}
if (back_ed != -1) {
int cur = v;
while (highest < dep[cur]) {
int e = par[cur];
cycle.push_back(e);
cur = n1[e] ^ n2[e] ^ cur;
}
cycle.push_back(back_ed);
}
for (int ed : g[v]) {
int u = n1[ed] ^ n2[ed] ^ v;
if (ed != par[u]) {
continue;
}
vector<int> now = calc(u);
if (now.empty()) {
continue;
}
int val = n;
for (int e : now) {
val = min(val, min(dep[n1[e]], dep[n2[e]]));
}
if (val < highest) {
cycle = now;
highest = val;
}
}
if (!cycle.empty()) {
handle_cycle(cycle);
} else {
if (par[v] != -1) {
assert(ret[par[v]] != 0);
ret[par[v]] = 1;
}
}
return cycle;
};
calc(0);
}
auto make_tree = [&](vector<int> edges) {
dsu d(n);
set<int> S;
int v = 0;
for (int e : edges) {
assert(d.unite(n1[e], n2[e]));
S.insert(e);
}
for (int e : tree_edges) {
assert(ret[e] != -1);
if (d.unite(n1[e], n2[e])) {
S.insert(e);
v += ret[e];
}
}
return make_pair(S, v);
};
function<void(set<int>)> calc = [&](set<int> nodes) {
if (nodes.size() == 1) {
return;
}
int divide = -1;
for (int e : tree_edges) {
if (nodes.find(n1[e]) != nodes.end() && nodes.find(n2[e]) != nodes.end()) {
divide = e;
}
}
assert(divide != -1);
dsu d(n);
for (int e : tree_edges) {
if (e != divide) {
d.unite(n1[e], n2[e]);
}
}
int a = n1[divide], b = n2[divide];
assert(d.get(a) != d.get(b));
set<int> A, B;
for (int i : nodes) {
if (d.get(a) == d.get(i)) {
A.insert(i);
} else {
assert(d.get(b) == d.get(i));
B.insert(i);
}
}
calc(A); calc(B);
if (A.size() > B.size()) {
swap(A, B);
}
vector<int> order;
for (int v : A) {
for (int e : g[v]) {
int u = n1[e] ^ n2[e] ^ v;
if (B.find(u) == B.end() || ret[e] != -1) {
continue;
}
order.push_back(e);
}
}
vector<vector<int>> forest;
set<pair<int, int>> dead;
for (int i = 0; i < (int) order.size(); i++) {
dead.insert({i, order[i]});
}
while (!dead.empty()) {
vector<int> now;
d = dsu(n);
vector<pair<int, int>> rev;
for (auto pe : dead) {
int e = pe.second;
if (ret[e] != -1) {
continue;
}
if (d.unite(n1[e], n2[e])) {
now.push_back(e);
rev.push_back(pe);
}
}
for (auto pe : rev) {
dead.erase(pe);
}
forest.push_back(now);
}
for (vector<int> edges : forest) {
int sz = edges.size();
auto pi = make_tree(edges);
int Valued = royal(pi.first) - pi.second;
if (Valued == 0) {
for (int e : edges) {
ret[e] = 0;
}
continue;
}
function<void(int, int, int)> solve = [&](int L, int R, int val) {
assert(val > 0);
if (L == R) {
assert(val <= 1);
assert(ret[edges[L]] == val || ret[edges[L]] == -1);
ret[edges[L]] = val;
return;
}
int mid = (L + R) >> 1;
vector<int> now;
for (int i = L; i <= mid; i++) {
now.push_back(edges[i]);
}
auto Pi = make_tree(now);
int t = royal(Pi.first) - Pi.second;
if (val - t > 0) {
solve(mid + 1, R, val - t);
} else {
for (int i = mid + 1; i <= R; i++) {
assert(ret[edges[i]] < 1);
ret[edges[i]] = 0;
}
}
if (t > 0) {
solve(L, mid, t);
} else {
for (int i = L; i <= mid; i++) {
assert(ret[edges[i]] < 1);
ret[edges[i]] = 0;
}
}
};
solve(0, sz - 1, Valued);
}
};
set<int> nodes;
for (int i = 0; i < n; i++) {
nodes.insert(i);
}
calc(nodes);
vector<int> res;
for (int i = 0; i < m; i++) {
assert(ret[i] != -1);
if (ret[i] == 1) {
res.push_back(i);
}
}
assert((int) res.size() == n - 1);
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
1 ms |
304 KB |
correct |
4 |
Correct |
1 ms |
212 KB |
correct |
5 |
Correct |
1 ms |
212 KB |
correct |
6 |
Correct |
1 ms |
212 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
296 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
212 KB |
correct |
12 |
Correct |
1 ms |
304 KB |
correct |
13 |
Correct |
1 ms |
296 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
1 ms |
304 KB |
correct |
4 |
Correct |
1 ms |
212 KB |
correct |
5 |
Correct |
1 ms |
212 KB |
correct |
6 |
Correct |
1 ms |
212 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
296 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
212 KB |
correct |
12 |
Correct |
1 ms |
304 KB |
correct |
13 |
Correct |
1 ms |
296 KB |
correct |
14 |
Correct |
5 ms |
300 KB |
correct |
15 |
Correct |
6 ms |
340 KB |
correct |
16 |
Correct |
5 ms |
412 KB |
correct |
17 |
Correct |
4 ms |
340 KB |
correct |
18 |
Correct |
3 ms |
340 KB |
correct |
19 |
Correct |
5 ms |
340 KB |
correct |
20 |
Correct |
4 ms |
304 KB |
correct |
21 |
Correct |
5 ms |
304 KB |
correct |
22 |
Correct |
3 ms |
340 KB |
correct |
23 |
Correct |
4 ms |
300 KB |
correct |
24 |
Correct |
3 ms |
300 KB |
correct |
25 |
Correct |
2 ms |
340 KB |
correct |
26 |
Correct |
3 ms |
340 KB |
correct |
27 |
Correct |
3 ms |
300 KB |
correct |
28 |
Correct |
2 ms |
340 KB |
correct |
29 |
Correct |
2 ms |
300 KB |
correct |
30 |
Correct |
3 ms |
340 KB |
correct |
31 |
Correct |
3 ms |
308 KB |
correct |
32 |
Correct |
3 ms |
340 KB |
correct |
33 |
Correct |
3 ms |
300 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
1 ms |
304 KB |
correct |
4 |
Correct |
1 ms |
212 KB |
correct |
5 |
Correct |
1 ms |
212 KB |
correct |
6 |
Correct |
1 ms |
212 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
296 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
212 KB |
correct |
12 |
Correct |
1 ms |
304 KB |
correct |
13 |
Correct |
1 ms |
296 KB |
correct |
14 |
Correct |
5 ms |
300 KB |
correct |
15 |
Correct |
6 ms |
340 KB |
correct |
16 |
Correct |
5 ms |
412 KB |
correct |
17 |
Correct |
4 ms |
340 KB |
correct |
18 |
Correct |
3 ms |
340 KB |
correct |
19 |
Correct |
5 ms |
340 KB |
correct |
20 |
Correct |
4 ms |
304 KB |
correct |
21 |
Correct |
5 ms |
304 KB |
correct |
22 |
Correct |
3 ms |
340 KB |
correct |
23 |
Correct |
4 ms |
300 KB |
correct |
24 |
Correct |
3 ms |
300 KB |
correct |
25 |
Correct |
2 ms |
340 KB |
correct |
26 |
Correct |
3 ms |
340 KB |
correct |
27 |
Correct |
3 ms |
300 KB |
correct |
28 |
Correct |
2 ms |
340 KB |
correct |
29 |
Correct |
2 ms |
300 KB |
correct |
30 |
Correct |
3 ms |
340 KB |
correct |
31 |
Correct |
3 ms |
308 KB |
correct |
32 |
Correct |
3 ms |
340 KB |
correct |
33 |
Correct |
3 ms |
300 KB |
correct |
34 |
Correct |
147 ms |
2144 KB |
correct |
35 |
Correct |
152 ms |
1828 KB |
correct |
36 |
Correct |
140 ms |
1752 KB |
correct |
37 |
Correct |
42 ms |
980 KB |
correct |
38 |
Correct |
144 ms |
1896 KB |
correct |
39 |
Correct |
154 ms |
1880 KB |
correct |
40 |
Correct |
125 ms |
1504 KB |
correct |
41 |
Correct |
84 ms |
1928 KB |
correct |
42 |
Correct |
129 ms |
2024 KB |
correct |
43 |
Correct |
79 ms |
1244 KB |
correct |
44 |
Correct |
71 ms |
1044 KB |
correct |
45 |
Correct |
72 ms |
1240 KB |
correct |
46 |
Correct |
64 ms |
1188 KB |
correct |
47 |
Correct |
58 ms |
996 KB |
correct |
48 |
Correct |
15 ms |
848 KB |
correct |
49 |
Correct |
47 ms |
736 KB |
correct |
50 |
Correct |
64 ms |
908 KB |
correct |
51 |
Correct |
70 ms |
1072 KB |
correct |
52 |
Correct |
65 ms |
1228 KB |
correct |
53 |
Correct |
70 ms |
972 KB |
correct |
54 |
Correct |
73 ms |
1452 KB |
correct |
55 |
Correct |
59 ms |
1276 KB |
correct |
56 |
Correct |
53 ms |
1272 KB |
correct |
57 |
Correct |
61 ms |
1268 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
610 ms |
4448 KB |
correct |
4 |
Correct |
982 ms |
7528 KB |
correct |
5 |
Correct |
942 ms |
6940 KB |
correct |
6 |
Correct |
893 ms |
6976 KB |
correct |
7 |
Correct |
932 ms |
7020 KB |
correct |
8 |
Correct |
747 ms |
6520 KB |
correct |
9 |
Correct |
982 ms |
6828 KB |
correct |
10 |
Correct |
953 ms |
6728 KB |
correct |
11 |
Correct |
879 ms |
6548 KB |
correct |
12 |
Correct |
928 ms |
6988 KB |
correct |
13 |
Correct |
1 ms |
212 KB |
correct |
14 |
Correct |
908 ms |
7100 KB |
correct |
15 |
Correct |
972 ms |
6692 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
1 ms |
304 KB |
correct |
4 |
Correct |
1 ms |
212 KB |
correct |
5 |
Correct |
1 ms |
212 KB |
correct |
6 |
Correct |
1 ms |
212 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
296 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
212 KB |
correct |
12 |
Correct |
1 ms |
304 KB |
correct |
13 |
Correct |
1 ms |
296 KB |
correct |
14 |
Correct |
5 ms |
300 KB |
correct |
15 |
Correct |
6 ms |
340 KB |
correct |
16 |
Correct |
5 ms |
412 KB |
correct |
17 |
Correct |
4 ms |
340 KB |
correct |
18 |
Correct |
3 ms |
340 KB |
correct |
19 |
Correct |
5 ms |
340 KB |
correct |
20 |
Correct |
4 ms |
304 KB |
correct |
21 |
Correct |
5 ms |
304 KB |
correct |
22 |
Correct |
3 ms |
340 KB |
correct |
23 |
Correct |
4 ms |
300 KB |
correct |
24 |
Correct |
3 ms |
300 KB |
correct |
25 |
Correct |
2 ms |
340 KB |
correct |
26 |
Correct |
3 ms |
340 KB |
correct |
27 |
Correct |
3 ms |
300 KB |
correct |
28 |
Correct |
2 ms |
340 KB |
correct |
29 |
Correct |
2 ms |
300 KB |
correct |
30 |
Correct |
3 ms |
340 KB |
correct |
31 |
Correct |
3 ms |
308 KB |
correct |
32 |
Correct |
3 ms |
340 KB |
correct |
33 |
Correct |
3 ms |
300 KB |
correct |
34 |
Correct |
147 ms |
2144 KB |
correct |
35 |
Correct |
152 ms |
1828 KB |
correct |
36 |
Correct |
140 ms |
1752 KB |
correct |
37 |
Correct |
42 ms |
980 KB |
correct |
38 |
Correct |
144 ms |
1896 KB |
correct |
39 |
Correct |
154 ms |
1880 KB |
correct |
40 |
Correct |
125 ms |
1504 KB |
correct |
41 |
Correct |
84 ms |
1928 KB |
correct |
42 |
Correct |
129 ms |
2024 KB |
correct |
43 |
Correct |
79 ms |
1244 KB |
correct |
44 |
Correct |
71 ms |
1044 KB |
correct |
45 |
Correct |
72 ms |
1240 KB |
correct |
46 |
Correct |
64 ms |
1188 KB |
correct |
47 |
Correct |
58 ms |
996 KB |
correct |
48 |
Correct |
15 ms |
848 KB |
correct |
49 |
Correct |
47 ms |
736 KB |
correct |
50 |
Correct |
64 ms |
908 KB |
correct |
51 |
Correct |
70 ms |
1072 KB |
correct |
52 |
Correct |
65 ms |
1228 KB |
correct |
53 |
Correct |
70 ms |
972 KB |
correct |
54 |
Correct |
73 ms |
1452 KB |
correct |
55 |
Correct |
59 ms |
1276 KB |
correct |
56 |
Correct |
53 ms |
1272 KB |
correct |
57 |
Correct |
61 ms |
1268 KB |
correct |
58 |
Correct |
0 ms |
212 KB |
correct |
59 |
Correct |
1 ms |
212 KB |
correct |
60 |
Correct |
610 ms |
4448 KB |
correct |
61 |
Correct |
982 ms |
7528 KB |
correct |
62 |
Correct |
942 ms |
6940 KB |
correct |
63 |
Correct |
893 ms |
6976 KB |
correct |
64 |
Correct |
932 ms |
7020 KB |
correct |
65 |
Correct |
747 ms |
6520 KB |
correct |
66 |
Correct |
982 ms |
6828 KB |
correct |
67 |
Correct |
953 ms |
6728 KB |
correct |
68 |
Correct |
879 ms |
6548 KB |
correct |
69 |
Correct |
928 ms |
6988 KB |
correct |
70 |
Correct |
1 ms |
212 KB |
correct |
71 |
Correct |
908 ms |
7100 KB |
correct |
72 |
Correct |
972 ms |
6692 KB |
correct |
73 |
Correct |
0 ms |
212 KB |
correct |
74 |
Correct |
961 ms |
6932 KB |
correct |
75 |
Correct |
938 ms |
7304 KB |
correct |
76 |
Correct |
472 ms |
3032 KB |
correct |
77 |
Correct |
1010 ms |
7816 KB |
correct |
78 |
Correct |
945 ms |
6584 KB |
correct |
79 |
Correct |
917 ms |
6552 KB |
correct |
80 |
Correct |
907 ms |
7016 KB |
correct |
81 |
Correct |
769 ms |
6084 KB |
correct |
82 |
Correct |
905 ms |
6452 KB |
correct |
83 |
Correct |
668 ms |
4232 KB |
correct |
84 |
Correct |
430 ms |
4680 KB |
correct |
85 |
Correct |
384 ms |
4068 KB |
correct |
86 |
Correct |
351 ms |
2888 KB |
correct |
87 |
Correct |
315 ms |
2324 KB |
correct |
88 |
Correct |
314 ms |
1848 KB |
correct |
89 |
Correct |
306 ms |
2276 KB |
correct |
90 |
Correct |
285 ms |
1816 KB |
correct |
91 |
Correct |
194 ms |
972 KB |
correct |
92 |
Correct |
74 ms |
1928 KB |
correct |
93 |
Correct |
369 ms |
4280 KB |
correct |
94 |
Correct |
358 ms |
2896 KB |
correct |
95 |
Correct |
345 ms |
2988 KB |
correct |
96 |
Correct |
326 ms |
1876 KB |
correct |
97 |
Correct |
290 ms |
2016 KB |
correct |
98 |
Correct |
333 ms |
2252 KB |
correct |
99 |
Correct |
288 ms |
2384 KB |
correct |
100 |
Correct |
233 ms |
1112 KB |
correct |
101 |
Correct |
106 ms |
976 KB |
correct |
102 |
Correct |
335 ms |
4096 KB |
correct |
103 |
Correct |
302 ms |
4056 KB |
correct |
104 |
Correct |
340 ms |
3780 KB |
correct |
105 |
Correct |
292 ms |
4152 KB |
correct |