#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int inf = 1e9;
int ori;
vector<vector<pii>> adj;
vector<vector<pii>> back_edges;
vector<vector<pii>> childs;
vector<int> color; // -1 : uncolored, 0 : normal edge, 1 : golden edge
vector<int> vis;
vector<pii> par;
vector<int> dep;
vector<int> query;
vector<int> ans;
int ask() { return count_common_roads(query); }
void ins(vector<int> v) {
for (auto &i : v)
query.push_back(i);
}
vector<int> chk;
void del(vector<int> v) {
for (auto &i : v) chk[i] = 1;
vector<int> t;
for (auto &i : query)
if (!chk[i])
t.push_back(i);
query = t;
for (auto &i : v) chk[i] = 0;
}
void make_dfs_tree(int x) {
vis[x] = 1;
for (auto &[y, id] : adj[x]) {
if (y == par[x].first) continue;
if (vis[y]) {
back_edges[x].emplace_back(y, id);
continue;
}
}
for (auto &[y, id] : adj[x]) {
if (vis[y]) continue;
childs[x].emplace_back(y, id);
par[y] = make_pair(x, id);
dep[y] = dep[x] + 1;
make_dfs_tree(y);
}
}
void color_dfs_tree(int x) {
if (!back_edges.empty()) {
int mn = inf;
pii e;
for (auto &[y, id] : back_edges[x]) {
if (mn > dep[y]) {
mn = dep[y];
e = make_pair(y, id);
}
}
if (mn < inf) {
vector<int> path;
int t = x;
int oth = -1;
while (t != e.first) {
if (color[par[t].second] == -1)
path.push_back(par[t].second);
else
oth = par[t].second;
t = par[t].first;
}
if (!path.empty()) {
vector<int> ret(path.size());
ins({e.second});
for (int i=0; i<(int)path.size(); i++) {
del({path[i]});
ret[i] = ask();
ins({path[i]});
}
del({e.second});
int d = 0;
if (oth == -1) {
for (int i=0; i<(int)path.size(); i++) {
if (ret[i] > ori) d = 1;
}
}
else {
ins({e.second});
del({oth});
d = ask() > ori-color[oth] ? 1 : 0;
del({e.second});
ins({oth});
}
for (int i=0; i<(int)path.size(); i++) {
color[path[i]] = ret[i]-d < ori ? 1 : 0;
}
}
}
}
for (auto &[y, id] : childs[x]) {
color_dfs_tree(y);
}
}
void color_back_edges(int x) {
if (!back_edges.empty()) {
sort(back_edges[x].begin(), back_edges[x].end(), [&] (pii u, pii v) { return dep[u.first] > dep[v.first]; });
vector<int> er(back_edges[x].size());
int idx = 0;
int t = x;
while (t > 0) {
if (idx < (int)back_edges[x].size() && par[t].first == back_edges[x][idx].first) {
er[idx] = par[t].second;
idx ++;
}
t = par[t].first;
}
int p = 0;
while (true) {
int lo = p, hi = (int)back_edges[x].size();
while (lo < hi) {
int mid = lo + hi >> 1;
int d = 0;
vector<int> t1, t2;
for (int i=p; i<=mid; i++) {
t1.push_back(back_edges[x][i].second);
t2.push_back(er[i]);
d += color[er[i]];
}
ins(t1);
del(t2);
if (ori - d < ask()) hi = mid;
else lo = mid + 1;
del(t1);
ins(t2);
}
if (lo == (int)back_edges[x].size()) break;
ans.push_back(back_edges[x][lo].second);
p = lo + 1;
}
}
for (auto &[y, id] : childs[x]) {
color_back_edges(y);
}
}
void init(int n, int m) {
adj.resize(n, vector<pii>());
back_edges.resize(n, vector<pii>());
childs.resize(n, vector<pii>());
color.resize(m, -1);
vis.resize(n, 0);
par.resize(n);
dep.resize(n);
chk.resize(m, 0);
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
int m = (int) u.size();
init(n, m);
for (int i=0; i<m; i++) {
adj[u[i]].emplace_back(v[i], i);
adj[v[i]].emplace_back(u[i], i);
}
par[0] = make_pair(-1,-1);
dep[0] = 0;
make_dfs_tree(0);
for (int i=1; i<n; i++) query.push_back(par[i].second);
ori = ask();
color_dfs_tree(0);
for (int i=1; i<n; i++) {
if (color[par[i].second] == -1) color[par[i].second] = 1;
if (color[par[i].second] == 1) ans.push_back(par[i].second);
}
color_back_edges(0);
assert((int)ans.size() == n-1);
return ans;
}
Compilation message
simurgh.cpp: In function 'void color_back_edges(int)':
simurgh.cpp:127:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
127 | int mid = lo + hi >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
correct |
2 |
Correct |
0 ms |
344 KB |
correct |
3 |
Correct |
0 ms |
344 KB |
correct |
4 |
Correct |
0 ms |
344 KB |
correct |
5 |
Correct |
1 ms |
344 KB |
correct |
6 |
Correct |
1 ms |
344 KB |
correct |
7 |
Correct |
0 ms |
344 KB |
correct |
8 |
Correct |
0 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
344 KB |
correct |
10 |
Correct |
1 ms |
432 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
344 KB |
correct |
13 |
Correct |
0 ms |
344 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
correct |
2 |
Correct |
0 ms |
344 KB |
correct |
3 |
Correct |
0 ms |
344 KB |
correct |
4 |
Correct |
0 ms |
344 KB |
correct |
5 |
Correct |
1 ms |
344 KB |
correct |
6 |
Correct |
1 ms |
344 KB |
correct |
7 |
Correct |
0 ms |
344 KB |
correct |
8 |
Correct |
0 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
344 KB |
correct |
10 |
Correct |
1 ms |
432 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
344 KB |
correct |
13 |
Correct |
0 ms |
344 KB |
correct |
14 |
Correct |
2 ms |
600 KB |
correct |
15 |
Correct |
1 ms |
344 KB |
correct |
16 |
Correct |
1 ms |
344 KB |
correct |
17 |
Correct |
2 ms |
344 KB |
correct |
18 |
Correct |
1 ms |
344 KB |
correct |
19 |
Correct |
1 ms |
344 KB |
correct |
20 |
Correct |
1 ms |
344 KB |
correct |
21 |
Correct |
2 ms |
344 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
2 ms |
344 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
1 ms |
344 KB |
correct |
26 |
Correct |
1 ms |
344 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
344 KB |
correct |
29 |
Correct |
1 ms |
344 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
344 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
correct |
2 |
Correct |
0 ms |
344 KB |
correct |
3 |
Correct |
0 ms |
344 KB |
correct |
4 |
Correct |
0 ms |
344 KB |
correct |
5 |
Correct |
1 ms |
344 KB |
correct |
6 |
Correct |
1 ms |
344 KB |
correct |
7 |
Correct |
0 ms |
344 KB |
correct |
8 |
Correct |
0 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
344 KB |
correct |
10 |
Correct |
1 ms |
432 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
344 KB |
correct |
13 |
Correct |
0 ms |
344 KB |
correct |
14 |
Correct |
2 ms |
600 KB |
correct |
15 |
Correct |
1 ms |
344 KB |
correct |
16 |
Correct |
1 ms |
344 KB |
correct |
17 |
Correct |
2 ms |
344 KB |
correct |
18 |
Correct |
1 ms |
344 KB |
correct |
19 |
Correct |
1 ms |
344 KB |
correct |
20 |
Correct |
1 ms |
344 KB |
correct |
21 |
Correct |
2 ms |
344 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
2 ms |
344 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
1 ms |
344 KB |
correct |
26 |
Correct |
1 ms |
344 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
344 KB |
correct |
29 |
Correct |
1 ms |
344 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
344 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 KB |
correct |
34 |
Correct |
21 ms |
2136 KB |
correct |
35 |
Correct |
21 ms |
2136 KB |
correct |
36 |
Correct |
17 ms |
1816 KB |
correct |
37 |
Correct |
5 ms |
344 KB |
correct |
38 |
Correct |
21 ms |
2392 KB |
correct |
39 |
Correct |
20 ms |
1880 KB |
correct |
40 |
Correct |
18 ms |
1624 KB |
correct |
41 |
Correct |
23 ms |
2136 KB |
correct |
42 |
Correct |
21 ms |
2136 KB |
correct |
43 |
Correct |
13 ms |
1368 KB |
correct |
44 |
Correct |
11 ms |
1112 KB |
correct |
45 |
Correct |
12 ms |
1112 KB |
correct |
46 |
Correct |
10 ms |
1112 KB |
correct |
47 |
Correct |
7 ms |
600 KB |
correct |
48 |
Correct |
2 ms |
344 KB |
correct |
49 |
Correct |
5 ms |
600 KB |
correct |
50 |
Correct |
8 ms |
600 KB |
correct |
51 |
Correct |
12 ms |
1112 KB |
correct |
52 |
Correct |
11 ms |
1112 KB |
correct |
53 |
Correct |
10 ms |
1112 KB |
correct |
54 |
Correct |
12 ms |
1624 KB |
correct |
55 |
Correct |
12 ms |
1368 KB |
correct |
56 |
Correct |
12 ms |
1368 KB |
correct |
57 |
Correct |
11 ms |
1356 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
correct |
2 |
Correct |
1 ms |
348 KB |
correct |
3 |
Correct |
59 ms |
5716 KB |
correct |
4 |
Correct |
93 ms |
7768 KB |
correct |
5 |
Correct |
92 ms |
7760 KB |
correct |
6 |
Correct |
89 ms |
7768 KB |
correct |
7 |
Correct |
100 ms |
8000 KB |
correct |
8 |
Correct |
96 ms |
7760 KB |
correct |
9 |
Correct |
93 ms |
8048 KB |
correct |
10 |
Correct |
92 ms |
7920 KB |
correct |
11 |
Correct |
100 ms |
7888 KB |
correct |
12 |
Correct |
92 ms |
7760 KB |
correct |
13 |
Correct |
0 ms |
344 KB |
correct |
14 |
Correct |
92 ms |
7964 KB |
correct |
15 |
Correct |
100 ms |
8264 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
correct |
2 |
Correct |
0 ms |
344 KB |
correct |
3 |
Correct |
0 ms |
344 KB |
correct |
4 |
Correct |
0 ms |
344 KB |
correct |
5 |
Correct |
1 ms |
344 KB |
correct |
6 |
Correct |
1 ms |
344 KB |
correct |
7 |
Correct |
0 ms |
344 KB |
correct |
8 |
Correct |
0 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
344 KB |
correct |
10 |
Correct |
1 ms |
432 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
344 KB |
correct |
13 |
Correct |
0 ms |
344 KB |
correct |
14 |
Correct |
2 ms |
600 KB |
correct |
15 |
Correct |
1 ms |
344 KB |
correct |
16 |
Correct |
1 ms |
344 KB |
correct |
17 |
Correct |
2 ms |
344 KB |
correct |
18 |
Correct |
1 ms |
344 KB |
correct |
19 |
Correct |
1 ms |
344 KB |
correct |
20 |
Correct |
1 ms |
344 KB |
correct |
21 |
Correct |
2 ms |
344 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
2 ms |
344 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
1 ms |
344 KB |
correct |
26 |
Correct |
1 ms |
344 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
344 KB |
correct |
29 |
Correct |
1 ms |
344 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
344 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 KB |
correct |
34 |
Correct |
21 ms |
2136 KB |
correct |
35 |
Correct |
21 ms |
2136 KB |
correct |
36 |
Correct |
17 ms |
1816 KB |
correct |
37 |
Correct |
5 ms |
344 KB |
correct |
38 |
Correct |
21 ms |
2392 KB |
correct |
39 |
Correct |
20 ms |
1880 KB |
correct |
40 |
Correct |
18 ms |
1624 KB |
correct |
41 |
Correct |
23 ms |
2136 KB |
correct |
42 |
Correct |
21 ms |
2136 KB |
correct |
43 |
Correct |
13 ms |
1368 KB |
correct |
44 |
Correct |
11 ms |
1112 KB |
correct |
45 |
Correct |
12 ms |
1112 KB |
correct |
46 |
Correct |
10 ms |
1112 KB |
correct |
47 |
Correct |
7 ms |
600 KB |
correct |
48 |
Correct |
2 ms |
344 KB |
correct |
49 |
Correct |
5 ms |
600 KB |
correct |
50 |
Correct |
8 ms |
600 KB |
correct |
51 |
Correct |
12 ms |
1112 KB |
correct |
52 |
Correct |
11 ms |
1112 KB |
correct |
53 |
Correct |
10 ms |
1112 KB |
correct |
54 |
Correct |
12 ms |
1624 KB |
correct |
55 |
Correct |
12 ms |
1368 KB |
correct |
56 |
Correct |
12 ms |
1368 KB |
correct |
57 |
Correct |
11 ms |
1356 KB |
correct |
58 |
Correct |
1 ms |
344 KB |
correct |
59 |
Correct |
1 ms |
348 KB |
correct |
60 |
Correct |
59 ms |
5716 KB |
correct |
61 |
Correct |
93 ms |
7768 KB |
correct |
62 |
Correct |
92 ms |
7760 KB |
correct |
63 |
Correct |
89 ms |
7768 KB |
correct |
64 |
Correct |
100 ms |
8000 KB |
correct |
65 |
Correct |
96 ms |
7760 KB |
correct |
66 |
Correct |
93 ms |
8048 KB |
correct |
67 |
Correct |
92 ms |
7920 KB |
correct |
68 |
Correct |
100 ms |
7888 KB |
correct |
69 |
Correct |
92 ms |
7760 KB |
correct |
70 |
Correct |
0 ms |
344 KB |
correct |
71 |
Correct |
92 ms |
7964 KB |
correct |
72 |
Correct |
100 ms |
8264 KB |
correct |
73 |
Correct |
0 ms |
344 KB |
correct |
74 |
Correct |
96 ms |
7888 KB |
correct |
75 |
Correct |
92 ms |
7712 KB |
correct |
76 |
Correct |
46 ms |
3276 KB |
correct |
77 |
Correct |
91 ms |
7760 KB |
correct |
78 |
Correct |
91 ms |
7764 KB |
correct |
79 |
Correct |
92 ms |
7856 KB |
correct |
80 |
Correct |
93 ms |
7760 KB |
correct |
81 |
Correct |
84 ms |
6992 KB |
correct |
82 |
Correct |
97 ms |
7944 KB |
correct |
83 |
Correct |
67 ms |
4440 KB |
correct |
84 |
Correct |
58 ms |
5456 KB |
correct |
85 |
Correct |
56 ms |
4952 KB |
correct |
86 |
Correct |
45 ms |
3408 KB |
correct |
87 |
Correct |
39 ms |
2652 KB |
correct |
88 |
Correct |
35 ms |
2128 KB |
correct |
89 |
Correct |
34 ms |
1980 KB |
correct |
90 |
Correct |
31 ms |
1884 KB |
correct |
91 |
Correct |
12 ms |
600 KB |
correct |
92 |
Correct |
6 ms |
344 KB |
correct |
93 |
Correct |
54 ms |
4952 KB |
correct |
94 |
Correct |
44 ms |
3408 KB |
correct |
95 |
Correct |
45 ms |
3408 KB |
correct |
96 |
Correct |
32 ms |
1880 KB |
correct |
97 |
Correct |
35 ms |
2140 KB |
correct |
98 |
Correct |
38 ms |
2648 KB |
correct |
99 |
Correct |
35 ms |
2136 KB |
correct |
100 |
Correct |
20 ms |
856 KB |
correct |
101 |
Correct |
6 ms |
600 KB |
correct |
102 |
Correct |
52 ms |
4184 KB |
correct |
103 |
Correct |
52 ms |
4268 KB |
correct |
104 |
Correct |
50 ms |
4184 KB |
correct |
105 |
Correct |
51 ms |
4232 KB |
correct |