#include <bits/stdc++.h>
#include "simurgh.h"
#define ar array
using namespace std;
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
struct dsu {
vector<int> p, size;
dsu(int n) : p(n), size(n, 1) {
iota(all(p), 0);
}
int get(int v) {
return v == p[v] ? v : p[v] = get(p[v]);
}
bool merge(int a, int b) {
a = get(a), b = get(b);
if (a == b) return 0;
if (size[a] < size[b]) swap(a, b);
size[a] += size[b], p[b] = a;
return 1;
}
};
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
int m = sz(u);
vector<vector<ar<int, 2>>> adj(n);
for (int i = 0; i < m; i++) {
adj[u[i]].push_back({v[i], i});
adj[v[i]].push_back({u[i], i});
}
vector<int> type(m, -1);
vector<ar<int, 2>> p(n, {-1, -1});
vector<int> tin(n), low(n);
int timer = 0;
vector<int> edges;
auto dfs = [&](auto&& s, int v) -> void {
tin[v] = low[v] = ++timer;
for (auto [u, idx] : adj[v]) if (u != p[v][0]) {
if (!tin[u]) {
p[u] = {v, idx}, edges.emplace_back(idx), s(s, u);
ckmin(low[v], low[u]);
if (low[u] > tin[v]) type[idx] = 1;
}
else {
ckmin(low[v], tin[u]);
}
}
};
dfs(dfs, 0);
int orig = count_common_roads(edges);
for (int v = 0; v < n; v++) {
for (auto [u, idx] : adj[v]) if (u != p[v][0] && tin[u] < tin[v]) {
//cout << "ASK " << v << " " << u << '\n';
vector<int> cur_edges;
for (int tmp = v; tmp != u; tmp = p[tmp][0]) {
int eidx = p[tmp][1];
cur_edges.emplace_back(eidx);
}
int not_gold = -1, gold = -1;
bool skip = 1;
for (int e : cur_edges) {
if (!~type[e]) skip = 0;
if (!type[e]) not_gold = e;
if (type[e] == 1) gold = e;
}
if (skip) continue;
if (~not_gold) {
//cout << "Hi not gold " << not_gold << '\n';
int orig2 = -1;
{
auto edges2 = edges;
edges2.erase(find(all(edges2), not_gold));
edges2.emplace_back(idx);
orig2 = count_common_roads(edges2);
}
for (int e : cur_edges) if (!~type[e]) {
auto edges2 = edges;
edges2.erase(find(all(edges2), e));
edges2.emplace_back(idx);
type[e] = count_common_roads(edges2) < orig2;
}
}
else if (~gold) {
//cout << "Hi " << gold << '\n';
int orig2 = -1;
{
auto edges2 = edges;
edges2.erase(find(all(edges2), gold));
edges2.emplace_back(idx);
orig2 = count_common_roads(edges2);
}
for (int e : cur_edges) if (!~type[e]) {
auto edges2 = edges;
edges2.erase(find(all(edges2), e));
edges2.emplace_back(idx);
type[e] = count_common_roads(edges2) == orig2;
}
}
else {
int mn = orig, mx = orig;
vector<int> val(sz(cur_edges));
assert(sz(val) > 1);
for (int i = 0; i < sz(cur_edges); i++) {
int e = cur_edges[i];
auto edges2 = edges;
edges2.erase(find(all(edges2), e));
edges2.emplace_back(idx);
int res = count_common_roads(edges2);
ckmin(mn, res), ckmax(mx, res);
val[i] = res;
}
/*
cout << "MN " << mn << " " << mx << '\n';
cout << "Edges\n";
for (int e : cur_edges) cout << e << " ";
cout << '\n';
*/
if (mn == mx) {
for (int e : cur_edges) type[e] = 0;
}
else {
assert(mx - 1 == mn);
for (int i = 0; i < sz(cur_edges); i++) {
//cout << "CUR " << cur_edges[i] << " " << val[i] << '\n';
type[cur_edges[i]] = val[i] < mx;
}
}
}
}
}
//for (int i = 0; i < m; i++) cout << u[i] << " " << v[i] << " " << type[i] << '\n';
auto ask = [&](vector<int> ve) {
dsu d(n);
vector<int> new_edges;
for (int e : ve) {
assert(d.merge(u[e], v[e]));
new_edges.emplace_back(e);
}
int cnt = 0;
for (int e : edges) {
if (d.merge(u[e], v[e])) {
new_edges.emplace_back(e);
cnt += type[e];
}
}
return count_common_roads(new_edges) > cnt;
};
vector<int> ans;
for (int i = 1; i < n; i++) {
vector<int> side;
for (auto [u, idx] : adj[i]) if (u < i) side.emplace_back(idx);
auto side2 = side;
while (1) {
if (side.empty() || !ask(side)) break;
while (side.size() > 1) {
vector<int> hf(side.begin(), side.begin() + side.size() / 2);
if (ask(hf)) swap(side, hf);
else side = vector<int>(side.begin() + side.size() / 2, side.end());
}
assert(side.size() == 1);
ans.emplace_back(side[0]);
side2.erase(find(all(side2), side[0]));
side = side2;
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
correct |
2 |
Correct |
0 ms |
344 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
0 ms |
348 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
344 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
correct |
2 |
Correct |
0 ms |
344 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
0 ms |
348 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
344 KB |
correct |
14 |
Correct |
2 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
348 KB |
correct |
16 |
Correct |
1 ms |
348 KB |
correct |
17 |
Correct |
1 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
1 ms |
348 KB |
correct |
20 |
Correct |
1 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
348 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
1 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
0 ms |
348 KB |
correct |
29 |
Correct |
0 ms |
348 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
348 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
344 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
correct |
2 |
Correct |
0 ms |
344 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
0 ms |
348 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
344 KB |
correct |
14 |
Correct |
2 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
348 KB |
correct |
16 |
Correct |
1 ms |
348 KB |
correct |
17 |
Correct |
1 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
1 ms |
348 KB |
correct |
20 |
Correct |
1 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
348 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
1 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
0 ms |
348 KB |
correct |
29 |
Correct |
0 ms |
348 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
348 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
344 KB |
correct |
34 |
Correct |
28 ms |
1804 KB |
correct |
35 |
Correct |
27 ms |
1624 KB |
correct |
36 |
Correct |
21 ms |
1624 KB |
correct |
37 |
Correct |
7 ms |
344 KB |
correct |
38 |
Correct |
26 ms |
1628 KB |
correct |
39 |
Correct |
25 ms |
1628 KB |
correct |
40 |
Correct |
21 ms |
1368 KB |
correct |
41 |
Correct |
30 ms |
1800 KB |
correct |
42 |
Correct |
24 ms |
1628 KB |
correct |
43 |
Correct |
16 ms |
1116 KB |
correct |
44 |
Correct |
14 ms |
860 KB |
correct |
45 |
Correct |
16 ms |
1112 KB |
correct |
46 |
Correct |
16 ms |
1000 KB |
correct |
47 |
Correct |
9 ms |
728 KB |
correct |
48 |
Correct |
3 ms |
476 KB |
correct |
49 |
Correct |
6 ms |
348 KB |
correct |
50 |
Correct |
9 ms |
736 KB |
correct |
51 |
Correct |
16 ms |
896 KB |
correct |
52 |
Correct |
14 ms |
860 KB |
correct |
53 |
Correct |
13 ms |
860 KB |
correct |
54 |
Correct |
16 ms |
1328 KB |
correct |
55 |
Correct |
18 ms |
1152 KB |
correct |
56 |
Correct |
17 ms |
1116 KB |
correct |
57 |
Correct |
18 ms |
1140 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
correct |
2 |
Correct |
0 ms |
348 KB |
correct |
3 |
Correct |
93 ms |
4472 KB |
correct |
4 |
Correct |
157 ms |
6160 KB |
correct |
5 |
Correct |
159 ms |
6144 KB |
correct |
6 |
Correct |
144 ms |
5976 KB |
correct |
7 |
Correct |
150 ms |
5980 KB |
correct |
8 |
Correct |
154 ms |
5980 KB |
correct |
9 |
Correct |
158 ms |
5932 KB |
correct |
10 |
Correct |
165 ms |
5980 KB |
correct |
11 |
Correct |
155 ms |
5976 KB |
correct |
12 |
Correct |
160 ms |
5980 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
153 ms |
6144 KB |
correct |
15 |
Correct |
160 ms |
5980 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
correct |
2 |
Correct |
0 ms |
344 KB |
correct |
3 |
Correct |
0 ms |
348 KB |
correct |
4 |
Correct |
0 ms |
348 KB |
correct |
5 |
Correct |
0 ms |
348 KB |
correct |
6 |
Correct |
0 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
0 ms |
348 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
344 KB |
correct |
14 |
Correct |
2 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
348 KB |
correct |
16 |
Correct |
1 ms |
348 KB |
correct |
17 |
Correct |
1 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
1 ms |
348 KB |
correct |
20 |
Correct |
1 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
348 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
1 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
0 ms |
348 KB |
correct |
29 |
Correct |
0 ms |
348 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
348 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
344 KB |
correct |
34 |
Correct |
28 ms |
1804 KB |
correct |
35 |
Correct |
27 ms |
1624 KB |
correct |
36 |
Correct |
21 ms |
1624 KB |
correct |
37 |
Correct |
7 ms |
344 KB |
correct |
38 |
Correct |
26 ms |
1628 KB |
correct |
39 |
Correct |
25 ms |
1628 KB |
correct |
40 |
Correct |
21 ms |
1368 KB |
correct |
41 |
Correct |
30 ms |
1800 KB |
correct |
42 |
Correct |
24 ms |
1628 KB |
correct |
43 |
Correct |
16 ms |
1116 KB |
correct |
44 |
Correct |
14 ms |
860 KB |
correct |
45 |
Correct |
16 ms |
1112 KB |
correct |
46 |
Correct |
16 ms |
1000 KB |
correct |
47 |
Correct |
9 ms |
728 KB |
correct |
48 |
Correct |
3 ms |
476 KB |
correct |
49 |
Correct |
6 ms |
348 KB |
correct |
50 |
Correct |
9 ms |
736 KB |
correct |
51 |
Correct |
16 ms |
896 KB |
correct |
52 |
Correct |
14 ms |
860 KB |
correct |
53 |
Correct |
13 ms |
860 KB |
correct |
54 |
Correct |
16 ms |
1328 KB |
correct |
55 |
Correct |
18 ms |
1152 KB |
correct |
56 |
Correct |
17 ms |
1116 KB |
correct |
57 |
Correct |
18 ms |
1140 KB |
correct |
58 |
Correct |
0 ms |
344 KB |
correct |
59 |
Correct |
0 ms |
348 KB |
correct |
60 |
Correct |
93 ms |
4472 KB |
correct |
61 |
Correct |
157 ms |
6160 KB |
correct |
62 |
Correct |
159 ms |
6144 KB |
correct |
63 |
Correct |
144 ms |
5976 KB |
correct |
64 |
Correct |
150 ms |
5980 KB |
correct |
65 |
Correct |
154 ms |
5980 KB |
correct |
66 |
Correct |
158 ms |
5932 KB |
correct |
67 |
Correct |
165 ms |
5980 KB |
correct |
68 |
Correct |
155 ms |
5976 KB |
correct |
69 |
Correct |
160 ms |
5980 KB |
correct |
70 |
Correct |
0 ms |
348 KB |
correct |
71 |
Correct |
153 ms |
6144 KB |
correct |
72 |
Correct |
160 ms |
5980 KB |
correct |
73 |
Correct |
1 ms |
344 KB |
correct |
74 |
Correct |
157 ms |
6100 KB |
correct |
75 |
Correct |
152 ms |
6020 KB |
correct |
76 |
Correct |
69 ms |
2396 KB |
correct |
77 |
Correct |
156 ms |
6012 KB |
correct |
78 |
Correct |
171 ms |
6224 KB |
correct |
79 |
Correct |
156 ms |
5980 KB |
correct |
80 |
Correct |
151 ms |
5984 KB |
correct |
81 |
Correct |
131 ms |
5468 KB |
correct |
82 |
Correct |
179 ms |
5980 KB |
correct |
83 |
Correct |
111 ms |
3416 KB |
correct |
84 |
Correct |
94 ms |
4300 KB |
correct |
85 |
Correct |
85 ms |
3880 KB |
correct |
86 |
Correct |
65 ms |
2648 KB |
correct |
87 |
Correct |
55 ms |
2368 KB |
correct |
88 |
Correct |
52 ms |
1624 KB |
correct |
89 |
Correct |
48 ms |
1628 KB |
correct |
90 |
Correct |
44 ms |
1552 KB |
correct |
91 |
Correct |
24 ms |
600 KB |
correct |
92 |
Correct |
14 ms |
528 KB |
correct |
93 |
Correct |
84 ms |
3928 KB |
correct |
94 |
Correct |
69 ms |
2652 KB |
correct |
95 |
Correct |
63 ms |
2904 KB |
correct |
96 |
Correct |
45 ms |
1624 KB |
correct |
97 |
Correct |
48 ms |
1628 KB |
correct |
98 |
Correct |
52 ms |
2140 KB |
correct |
99 |
Correct |
48 ms |
1628 KB |
correct |
100 |
Correct |
31 ms |
604 KB |
correct |
101 |
Correct |
15 ms |
348 KB |
correct |
102 |
Correct |
98 ms |
3352 KB |
correct |
103 |
Correct |
109 ms |
3420 KB |
correct |
104 |
Correct |
99 ms |
3332 KB |
correct |
105 |
Correct |
98 ms |
3164 KB |
correct |