#include <bits/stdc++.h>
#include "simurgh.h"
using namespace std;
template <class F> struct rec_lambda : private F {
explicit rec_lambda(F&& f) : F(forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, forward<Args>(args)...);
}
};
vector<int> find_roads(int N, vector<int> U, vector<int> V) {
const int M = U.size();
vector<vector<int>> graph(N);
for (int i = 0; i < M; ++i) {
graph[U[i]].push_back(i);
graph[V[i]].push_back(i);
}
const auto dst = [&](const int u, const int e) {
return U[e] ^ V[e] ^ u;
};
vector<int> depth(N), pedge(N);
vector<char> is_tree(M);
depth[0] = 1;
rec_lambda([&](auto&& dfs, const int u) -> void {
for (const int e : graph[u]) {
const int v = dst(u, e);
if (depth[v] == 0) {
depth[v] = depth[u] + 1;
pedge[v] = e;
is_tree[e] = true;
dfs(v);
}
}
})(0);
for (int i = 0; i < M; ++i) {
if (depth[U[i]] > depth[V[i]]) swap(U[i], V[i]);
}
vector<int> ask_list;
ask_list.reserve(N - 1);
const auto ask_tree = [&] {
ask_list.clear();
for (int i = 0; i < M; ++i) {
if (is_tree[i]) ask_list.push_back(i);
}
return count_common_roads(ask_list);
};
const auto ask_swapped = [&](const int e, const int f) {
is_tree[e] = true;
is_tree[f] = false;
const int ret = ask_tree();
is_tree[e] = false;
is_tree[f] = true;
return ret;
};
const int base = ask_tree();
vector<int> type(M, -1);
for (int e = 0; e < M; ++e) {
if (is_tree[e]) continue;
vector<int> done, tbd;
for (int u = V[e]; u != U[e]; u = dst(u, pedge[u])) {
const int f = pedge[u];
(type[f] >= 0 ? done : tbd).push_back(f);
}
const int m = tbd.size();
if (m == 0) continue;
vector<int> dif(m);
for (int i = 0; i < m; ++i) {
dif[i] = ask_swapped(e, tbd[i]) - base;
}
for (const int x : dif) {
if (x == 1) type[e] = 1;
if (x == -1) type[e] = 0;
}
if (type[e] == -1) {
if (done.empty()) {
type[e] = 0;
} else {
const int f = done.front();
type[e] = type[f] + ask_swapped(e, f) - base;
}
}
for (int i = 0; i < m; ++i) {
type[tbd[i]] = type[e] - dif[i];
}
}
for (int i = 0; i < M; ++i) {
if (is_tree[i] and type[i] == -1) type[i] = 1;
}
vector<char> used(N);
const auto count = [&](const vector<int>& list) {
if (list.empty()) return 0;
ask_list.clear();
for (const int e : list) {
ask_list.push_back(e);
used[V[e]] = true;
}
int subt = 0;
for (int u = 1; u < N; ++u) {
if (!used[u]) {
const int e = pedge[u];
subt += type[e];
ask_list.push_back(e);
}
}
for (const int e : list) {
used[V[e]] = false;
}
return count_common_roads(ask_list) - subt;
};
for (int pivot = 0; pivot < N; ++pivot) {
vector<int> list;
for (const int e : graph[pivot]) {
if (type[e] == -1 and U[e] == pivot) list.push_back(e);
}
rec_lambda([&](auto&& dfs, const int l, const int r, const int all) -> void {
if (all == 0) return;
if (all == r - l) {
for (int i = l; i < r; ++i) type[list[i]] = 1;
return;
}
const int m = (l + r) / 2;
const int left = count(vector<int>(list.begin() + l, list.begin() + m));
dfs(l, m, left);
dfs(m, r, all - left);
})(0, list.size(), count(list));
for (const int e : list) {
if (type[e] == -1) type[e] = 0;
}
}
vector<int> ret;
ret.reserve(N - 1);
for (int i = 0; i < M; ++i) {
if (type[i] == 1) ret.push_back(i);
}
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
0 ms |
212 KB |
correct |
4 |
Correct |
0 ms |
212 KB |
correct |
5 |
Correct |
0 ms |
212 KB |
correct |
6 |
Correct |
0 ms |
212 KB |
correct |
7 |
Correct |
0 ms |
212 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
212 KB |
correct |
10 |
Correct |
0 ms |
212 KB |
correct |
11 |
Correct |
0 ms |
212 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
0 ms |
212 KB |
correct |
4 |
Correct |
0 ms |
212 KB |
correct |
5 |
Correct |
0 ms |
212 KB |
correct |
6 |
Correct |
0 ms |
212 KB |
correct |
7 |
Correct |
0 ms |
212 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
212 KB |
correct |
10 |
Correct |
0 ms |
212 KB |
correct |
11 |
Correct |
0 ms |
212 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
2 ms |
340 KB |
correct |
15 |
Correct |
2 ms |
340 KB |
correct |
16 |
Correct |
1 ms |
340 KB |
correct |
17 |
Correct |
1 ms |
340 KB |
correct |
18 |
Correct |
1 ms |
212 KB |
correct |
19 |
Correct |
2 ms |
340 KB |
correct |
20 |
Correct |
1 ms |
212 KB |
correct |
21 |
Correct |
1 ms |
212 KB |
correct |
22 |
Correct |
1 ms |
212 KB |
correct |
23 |
Correct |
1 ms |
212 KB |
correct |
24 |
Correct |
1 ms |
212 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
212 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 |
212 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
0 ms |
212 KB |
correct |
4 |
Correct |
0 ms |
212 KB |
correct |
5 |
Correct |
0 ms |
212 KB |
correct |
6 |
Correct |
0 ms |
212 KB |
correct |
7 |
Correct |
0 ms |
212 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
212 KB |
correct |
10 |
Correct |
0 ms |
212 KB |
correct |
11 |
Correct |
0 ms |
212 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
2 ms |
340 KB |
correct |
15 |
Correct |
2 ms |
340 KB |
correct |
16 |
Correct |
1 ms |
340 KB |
correct |
17 |
Correct |
1 ms |
340 KB |
correct |
18 |
Correct |
1 ms |
212 KB |
correct |
19 |
Correct |
2 ms |
340 KB |
correct |
20 |
Correct |
1 ms |
212 KB |
correct |
21 |
Correct |
1 ms |
212 KB |
correct |
22 |
Correct |
1 ms |
212 KB |
correct |
23 |
Correct |
1 ms |
212 KB |
correct |
24 |
Correct |
1 ms |
212 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
212 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 |
212 KB |
correct |
34 |
Correct |
60 ms |
1176 KB |
correct |
35 |
Correct |
56 ms |
1108 KB |
correct |
36 |
Correct |
43 ms |
952 KB |
correct |
37 |
Correct |
6 ms |
364 KB |
correct |
38 |
Correct |
62 ms |
1108 KB |
correct |
39 |
Correct |
49 ms |
1096 KB |
correct |
40 |
Correct |
42 ms |
1100 KB |
correct |
41 |
Correct |
58 ms |
1176 KB |
correct |
42 |
Correct |
54 ms |
1176 KB |
correct |
43 |
Correct |
25 ms |
872 KB |
correct |
44 |
Correct |
20 ms |
596 KB |
correct |
45 |
Correct |
23 ms |
724 KB |
correct |
46 |
Correct |
18 ms |
596 KB |
correct |
47 |
Correct |
9 ms |
500 KB |
correct |
48 |
Correct |
2 ms |
212 KB |
correct |
49 |
Correct |
4 ms |
340 KB |
correct |
50 |
Correct |
9 ms |
504 KB |
correct |
51 |
Correct |
25 ms |
756 KB |
correct |
52 |
Correct |
20 ms |
596 KB |
correct |
53 |
Correct |
18 ms |
660 KB |
correct |
54 |
Correct |
25 ms |
852 KB |
correct |
55 |
Correct |
27 ms |
760 KB |
correct |
56 |
Correct |
28 ms |
676 KB |
correct |
57 |
Correct |
28 ms |
756 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
226 ms |
2852 KB |
correct |
4 |
Correct |
420 ms |
4224 KB |
correct |
5 |
Correct |
431 ms |
4172 KB |
correct |
6 |
Correct |
374 ms |
3972 KB |
correct |
7 |
Correct |
414 ms |
4060 KB |
correct |
8 |
Correct |
410 ms |
4060 KB |
correct |
9 |
Correct |
429 ms |
4056 KB |
correct |
10 |
Correct |
429 ms |
4052 KB |
correct |
11 |
Correct |
436 ms |
4064 KB |
correct |
12 |
Correct |
427 ms |
4056 KB |
correct |
13 |
Correct |
1 ms |
212 KB |
correct |
14 |
Correct |
431 ms |
4056 KB |
correct |
15 |
Correct |
444 ms |
4056 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
0 ms |
212 KB |
correct |
4 |
Correct |
0 ms |
212 KB |
correct |
5 |
Correct |
0 ms |
212 KB |
correct |
6 |
Correct |
0 ms |
212 KB |
correct |
7 |
Correct |
0 ms |
212 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
0 ms |
212 KB |
correct |
10 |
Correct |
0 ms |
212 KB |
correct |
11 |
Correct |
0 ms |
212 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
2 ms |
340 KB |
correct |
15 |
Correct |
2 ms |
340 KB |
correct |
16 |
Correct |
1 ms |
340 KB |
correct |
17 |
Correct |
1 ms |
340 KB |
correct |
18 |
Correct |
1 ms |
212 KB |
correct |
19 |
Correct |
2 ms |
340 KB |
correct |
20 |
Correct |
1 ms |
212 KB |
correct |
21 |
Correct |
1 ms |
212 KB |
correct |
22 |
Correct |
1 ms |
212 KB |
correct |
23 |
Correct |
1 ms |
212 KB |
correct |
24 |
Correct |
1 ms |
212 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
212 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 |
212 KB |
correct |
34 |
Correct |
60 ms |
1176 KB |
correct |
35 |
Correct |
56 ms |
1108 KB |
correct |
36 |
Correct |
43 ms |
952 KB |
correct |
37 |
Correct |
6 ms |
364 KB |
correct |
38 |
Correct |
62 ms |
1108 KB |
correct |
39 |
Correct |
49 ms |
1096 KB |
correct |
40 |
Correct |
42 ms |
1100 KB |
correct |
41 |
Correct |
58 ms |
1176 KB |
correct |
42 |
Correct |
54 ms |
1176 KB |
correct |
43 |
Correct |
25 ms |
872 KB |
correct |
44 |
Correct |
20 ms |
596 KB |
correct |
45 |
Correct |
23 ms |
724 KB |
correct |
46 |
Correct |
18 ms |
596 KB |
correct |
47 |
Correct |
9 ms |
500 KB |
correct |
48 |
Correct |
2 ms |
212 KB |
correct |
49 |
Correct |
4 ms |
340 KB |
correct |
50 |
Correct |
9 ms |
504 KB |
correct |
51 |
Correct |
25 ms |
756 KB |
correct |
52 |
Correct |
20 ms |
596 KB |
correct |
53 |
Correct |
18 ms |
660 KB |
correct |
54 |
Correct |
25 ms |
852 KB |
correct |
55 |
Correct |
27 ms |
760 KB |
correct |
56 |
Correct |
28 ms |
676 KB |
correct |
57 |
Correct |
28 ms |
756 KB |
correct |
58 |
Correct |
0 ms |
212 KB |
correct |
59 |
Correct |
1 ms |
212 KB |
correct |
60 |
Correct |
226 ms |
2852 KB |
correct |
61 |
Correct |
420 ms |
4224 KB |
correct |
62 |
Correct |
431 ms |
4172 KB |
correct |
63 |
Correct |
374 ms |
3972 KB |
correct |
64 |
Correct |
414 ms |
4060 KB |
correct |
65 |
Correct |
410 ms |
4060 KB |
correct |
66 |
Correct |
429 ms |
4056 KB |
correct |
67 |
Correct |
429 ms |
4052 KB |
correct |
68 |
Correct |
436 ms |
4064 KB |
correct |
69 |
Correct |
427 ms |
4056 KB |
correct |
70 |
Correct |
1 ms |
212 KB |
correct |
71 |
Correct |
431 ms |
4056 KB |
correct |
72 |
Correct |
444 ms |
4056 KB |
correct |
73 |
Correct |
0 ms |
212 KB |
correct |
74 |
Correct |
443 ms |
4044 KB |
correct |
75 |
Correct |
413 ms |
4860 KB |
correct |
76 |
Correct |
144 ms |
2004 KB |
correct |
77 |
Correct |
448 ms |
5000 KB |
correct |
78 |
Correct |
431 ms |
4996 KB |
correct |
79 |
Correct |
447 ms |
4996 KB |
correct |
80 |
Correct |
414 ms |
4812 KB |
correct |
81 |
Correct |
340 ms |
4300 KB |
correct |
82 |
Correct |
410 ms |
4852 KB |
correct |
83 |
Correct |
238 ms |
2820 KB |
correct |
84 |
Correct |
202 ms |
3296 KB |
correct |
85 |
Correct |
176 ms |
3020 KB |
correct |
86 |
Correct |
125 ms |
2052 KB |
correct |
87 |
Correct |
90 ms |
1620 KB |
correct |
88 |
Correct |
66 ms |
1248 KB |
correct |
89 |
Correct |
65 ms |
1204 KB |
correct |
90 |
Correct |
59 ms |
1136 KB |
correct |
91 |
Correct |
14 ms |
440 KB |
correct |
92 |
Correct |
7 ms |
368 KB |
correct |
93 |
Correct |
173 ms |
3096 KB |
correct |
94 |
Correct |
123 ms |
2128 KB |
correct |
95 |
Correct |
114 ms |
2104 KB |
correct |
96 |
Correct |
59 ms |
1184 KB |
correct |
97 |
Correct |
77 ms |
1304 KB |
correct |
98 |
Correct |
99 ms |
1720 KB |
correct |
99 |
Correct |
70 ms |
1236 KB |
correct |
100 |
Correct |
25 ms |
468 KB |
correct |
101 |
Correct |
8 ms |
340 KB |
correct |
102 |
Correct |
200 ms |
2892 KB |
correct |
103 |
Correct |
199 ms |
2696 KB |
correct |
104 |
Correct |
194 ms |
2644 KB |
correct |
105 |
Correct |
199 ms |
2664 KB |
correct |