#include "simurgh.h"
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 505;
int n, m;
int ad[mxN][mxN];
vector<int> g[mxN];
int cnt;
vector<pair<int, int>> edges;
struct DSU {
int n;
vector<int> par;
DSU(int _n) {
n = _n;
par.resize(n);
iota(par.begin(), par.end(), 0);
}
int find(int u) {
return par[u] = (u == par[u] ? u : find(par[u]));
}
bool unite(int u, int v) {
u = find(u), v = find(v);
if (u == v) return false;
par[u] = v;
return true;
}
};
int query(vector<int> r) {
cnt++;
return count_common_roads(r);
}
set<int> tree;
set<int> on;
int Myquery(vector<int> v) {
vector<int> r;
DSU dsu(n);
for (int i : v) {
auto [u, v] = edges[i];
r.push_back(i);
dsu.unite(u, v);
}
for (int i : on) {
auto [u, v] = edges[i];
if (dsu.unite(u, v)) r.push_back(i);
}
for (int i : tree) {
auto [u, v] = edges[i];
if (dsu.unite(u, v)) r.push_back(i);
}
return query(r);
}
int init;
int reduction(vector<int> v) {
vector<int> r;
DSU dsu(n);
for (int i : v) {
auto [u, v] = edges[i];
r.push_back(i);
dsu.unite(u, v);
}
int ans = 0;
for (int i : on) {
auto [u, v] = edges[i];
if (dsu.unite(u, v)) r.push_back(i);
else ans++;
}
for (int i : tree) {
auto [u, v] = edges[i];
if (dsu.unite(u, v)) r.push_back(i);
}
return ans;
}
bool vis[mxN];
int in[mxN], out[mxN], par[mxN], timer;
void find_tree(int u) {
in[u] = ++timer;
vis[u] = true;
for (int v = 0; v < n; v++) {
if (ad[u][v] != -1 && !vis[v]) {
g[u].push_back(v);
tree.insert(ad[u][v]);
par[v] = u;
find_tree(v);
}
}
out[u] = timer;
}
bool done[mxN * mxN];
vector<int> Q;
set<int> new_on;
void solve(int l, int r, int tot) {
if (l > r || !tot) return;
if (l == r) {
new_on.insert(Q[l]);
return;
}
int m = (l + r) >> 1;
int val = Myquery(vector<int> (Q.begin() + l, Q.begin() + m + 1));
int red = reduction(vector<int> (Q.begin() + l, Q.begin() + m + 1));
solve(l, m, val + red - init);
solve(m + 1, r, tot - (val + red - init));
}
void solve_u(int u) {
vector<int> cur;
for (int i = 0; i < n; i++) {
if (ad[u][i] != -1 && !done[ad[u][i]]) cur.push_back(ad[u][i]);
}
Q = cur;
solve(0, Q.size() - 1, Myquery(Q) + reduction(Q) - init);
for (int i : Q) done[i] = true;
}
bool insubtree(int u, int v) {
return (in[u] <= in[v] && out[v] <= out[u]);
}
void get(int u, int v, int a, int b) {
vector<int> path = {ad[a][b]};
int aa = a;
while (aa != b) {
path.push_back(ad[aa][par[aa]]);
aa = par[aa];
}
int val = -1;
for (int i : path) {
if (done[i]) {
set<int> ntree = tree;
ntree.erase(i);
ntree.insert(ad[a][b]);
int v = query(vector<int> (ntree.begin(), ntree.end()));
if (v == init) val = on.count(i);
else val = on.count(i) ^ 1;
break;
}
}
vector<int> store(path.size(), -1);
if (val == -1) {
int mx = init;
set<int> ntree = tree;
ntree.insert(ad[a][b]);
int idx = 1;
for (int i : path) {
if (i == ad[a][b]) continue;
ntree.erase(i);
mx = max(mx, store[idx] = query(vector<int> (ntree.begin(), ntree.end())));
ntree.insert(i);
idx++;
}
if (mx > init) val = 1;
else val = 0;
}
if (val == 1) new_on.insert(ad[a][b]);
for (int i = 1; i < path.size(); i++) {
if (done[path[i]]) continue;
auto ntree = tree;
ntree.insert(ad[a][b]);
ntree.erase(path[i]);
int q = (store[i] == -1 ? query(vector<int> (ntree.begin(), ntree.end())) : store[i]);
if (val == 1) {
if (q == init) on.insert(path[i]);
} else {
if (q < init) on.insert(path[i]);
}
}
for (int i : path) done[i] = true;
}
void find_tree_edges() {
find_tree(0);
init = query(vector<int> (tree.begin(), tree.end()));
for (int u = 0; u < n; u++) {
for (int v : g[u]) {
if (done[ad[u][v]]) continue;
for (int i = 0; i < m; i++) {
auto [a, b] = edges[i];
if (i == ad[u][v]) continue;
if (insubtree(v, a) && !insubtree(v, b)) {
get(u, v, a, b);
break;
}
if (insubtree(v, b) && !insubtree(v, a)) {
get(u, v, b, a);
break;
}
}
if (!done[ad[u][v]]) {
done[ad[u][v]] = true;
on.insert(ad[u][v]);
}
}
}
}
vector<int> find_roads(int N, vector<int> U, vector<int> V) {
memset(par, -1, sizeof par);
if (N == 2) {
return {0};
}
memset(ad, -1, sizeof ad);
n = N, m = U.size();
for (int i = 0; i < m; i++) {
ad[U[i]][V[i]] = ad[V[i]][U[i]] = i;
edges.push_back({U[i], V[i]});
}
find_tree_edges();
for (int u = 0; u < n; u++) {
solve_u(u);
}
for (int i : new_on) on.insert(i);
return vector<int> (on.begin(), on.end());
}
Compilation message
simurgh.cpp: In function 'void get(int, int, int, int)':
simurgh.cpp:154:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | for (int i = 1; i < path.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
correct |
2 |
Correct |
1 ms |
1236 KB |
correct |
3 |
Correct |
1 ms |
1236 KB |
correct |
4 |
Correct |
1 ms |
1200 KB |
correct |
5 |
Correct |
1 ms |
1236 KB |
correct |
6 |
Correct |
1 ms |
1236 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
1236 KB |
correct |
9 |
Correct |
1 ms |
1236 KB |
correct |
10 |
Correct |
1 ms |
1236 KB |
correct |
11 |
Correct |
1 ms |
1236 KB |
correct |
12 |
Correct |
1 ms |
1236 KB |
correct |
13 |
Correct |
1 ms |
1236 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
correct |
2 |
Correct |
1 ms |
1236 KB |
correct |
3 |
Correct |
1 ms |
1236 KB |
correct |
4 |
Correct |
1 ms |
1200 KB |
correct |
5 |
Correct |
1 ms |
1236 KB |
correct |
6 |
Correct |
1 ms |
1236 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
1236 KB |
correct |
9 |
Correct |
1 ms |
1236 KB |
correct |
10 |
Correct |
1 ms |
1236 KB |
correct |
11 |
Correct |
1 ms |
1236 KB |
correct |
12 |
Correct |
1 ms |
1236 KB |
correct |
13 |
Correct |
1 ms |
1236 KB |
correct |
14 |
Correct |
3 ms |
1364 KB |
correct |
15 |
Correct |
2 ms |
1364 KB |
correct |
16 |
Correct |
2 ms |
1364 KB |
correct |
17 |
Correct |
3 ms |
1236 KB |
correct |
18 |
Correct |
3 ms |
1236 KB |
correct |
19 |
Correct |
3 ms |
1364 KB |
correct |
20 |
Correct |
3 ms |
1236 KB |
correct |
21 |
Correct |
3 ms |
1236 KB |
correct |
22 |
Correct |
2 ms |
1236 KB |
correct |
23 |
Correct |
3 ms |
1236 KB |
correct |
24 |
Correct |
2 ms |
1236 KB |
correct |
25 |
Correct |
1 ms |
1236 KB |
correct |
26 |
Correct |
2 ms |
1236 KB |
correct |
27 |
Correct |
3 ms |
1236 KB |
correct |
28 |
Correct |
1 ms |
1236 KB |
correct |
29 |
Correct |
2 ms |
1236 KB |
correct |
30 |
Correct |
2 ms |
1236 KB |
correct |
31 |
Correct |
2 ms |
1236 KB |
correct |
32 |
Correct |
2 ms |
1236 KB |
correct |
33 |
Correct |
3 ms |
1236 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
correct |
2 |
Correct |
1 ms |
1236 KB |
correct |
3 |
Correct |
1 ms |
1236 KB |
correct |
4 |
Correct |
1 ms |
1200 KB |
correct |
5 |
Correct |
1 ms |
1236 KB |
correct |
6 |
Correct |
1 ms |
1236 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
1236 KB |
correct |
9 |
Correct |
1 ms |
1236 KB |
correct |
10 |
Correct |
1 ms |
1236 KB |
correct |
11 |
Correct |
1 ms |
1236 KB |
correct |
12 |
Correct |
1 ms |
1236 KB |
correct |
13 |
Correct |
1 ms |
1236 KB |
correct |
14 |
Correct |
3 ms |
1364 KB |
correct |
15 |
Correct |
2 ms |
1364 KB |
correct |
16 |
Correct |
2 ms |
1364 KB |
correct |
17 |
Correct |
3 ms |
1236 KB |
correct |
18 |
Correct |
3 ms |
1236 KB |
correct |
19 |
Correct |
3 ms |
1364 KB |
correct |
20 |
Correct |
3 ms |
1236 KB |
correct |
21 |
Correct |
3 ms |
1236 KB |
correct |
22 |
Correct |
2 ms |
1236 KB |
correct |
23 |
Correct |
3 ms |
1236 KB |
correct |
24 |
Correct |
2 ms |
1236 KB |
correct |
25 |
Correct |
1 ms |
1236 KB |
correct |
26 |
Correct |
2 ms |
1236 KB |
correct |
27 |
Correct |
3 ms |
1236 KB |
correct |
28 |
Correct |
1 ms |
1236 KB |
correct |
29 |
Correct |
2 ms |
1236 KB |
correct |
30 |
Correct |
2 ms |
1236 KB |
correct |
31 |
Correct |
2 ms |
1236 KB |
correct |
32 |
Correct |
2 ms |
1236 KB |
correct |
33 |
Correct |
3 ms |
1236 KB |
correct |
34 |
Correct |
60 ms |
2080 KB |
correct |
35 |
Correct |
52 ms |
2132 KB |
correct |
36 |
Correct |
48 ms |
2004 KB |
correct |
37 |
Correct |
18 ms |
1416 KB |
correct |
38 |
Correct |
52 ms |
2124 KB |
correct |
39 |
Correct |
44 ms |
2004 KB |
correct |
40 |
Correct |
45 ms |
2004 KB |
correct |
41 |
Correct |
55 ms |
2132 KB |
correct |
42 |
Correct |
57 ms |
2132 KB |
correct |
43 |
Correct |
41 ms |
1848 KB |
correct |
44 |
Correct |
36 ms |
1772 KB |
correct |
45 |
Correct |
46 ms |
1748 KB |
correct |
46 |
Correct |
38 ms |
1648 KB |
correct |
47 |
Correct |
28 ms |
1512 KB |
correct |
48 |
Correct |
12 ms |
1364 KB |
correct |
49 |
Correct |
17 ms |
1412 KB |
correct |
50 |
Correct |
27 ms |
1492 KB |
correct |
51 |
Correct |
41 ms |
1748 KB |
correct |
52 |
Correct |
37 ms |
1768 KB |
correct |
53 |
Correct |
35 ms |
1628 KB |
correct |
54 |
Correct |
41 ms |
1836 KB |
correct |
55 |
Correct |
43 ms |
1748 KB |
correct |
56 |
Correct |
47 ms |
1748 KB |
correct |
57 |
Correct |
44 ms |
1748 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
232 KB |
correct |
2 |
Correct |
1 ms |
1236 KB |
correct |
3 |
Correct |
159 ms |
3616 KB |
correct |
4 |
Correct |
254 ms |
4564 KB |
correct |
5 |
Correct |
290 ms |
4568 KB |
correct |
6 |
Correct |
242 ms |
4616 KB |
correct |
7 |
Correct |
166 ms |
4564 KB |
correct |
8 |
Correct |
181 ms |
4564 KB |
correct |
9 |
Correct |
249 ms |
4564 KB |
correct |
10 |
Correct |
275 ms |
4568 KB |
correct |
11 |
Correct |
285 ms |
4580 KB |
correct |
12 |
Correct |
263 ms |
4672 KB |
correct |
13 |
Correct |
1 ms |
1236 KB |
correct |
14 |
Correct |
249 ms |
4568 KB |
correct |
15 |
Correct |
253 ms |
4568 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
correct |
2 |
Correct |
1 ms |
1236 KB |
correct |
3 |
Correct |
1 ms |
1236 KB |
correct |
4 |
Correct |
1 ms |
1200 KB |
correct |
5 |
Correct |
1 ms |
1236 KB |
correct |
6 |
Correct |
1 ms |
1236 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
1236 KB |
correct |
9 |
Correct |
1 ms |
1236 KB |
correct |
10 |
Correct |
1 ms |
1236 KB |
correct |
11 |
Correct |
1 ms |
1236 KB |
correct |
12 |
Correct |
1 ms |
1236 KB |
correct |
13 |
Correct |
1 ms |
1236 KB |
correct |
14 |
Correct |
3 ms |
1364 KB |
correct |
15 |
Correct |
2 ms |
1364 KB |
correct |
16 |
Correct |
2 ms |
1364 KB |
correct |
17 |
Correct |
3 ms |
1236 KB |
correct |
18 |
Correct |
3 ms |
1236 KB |
correct |
19 |
Correct |
3 ms |
1364 KB |
correct |
20 |
Correct |
3 ms |
1236 KB |
correct |
21 |
Correct |
3 ms |
1236 KB |
correct |
22 |
Correct |
2 ms |
1236 KB |
correct |
23 |
Correct |
3 ms |
1236 KB |
correct |
24 |
Correct |
2 ms |
1236 KB |
correct |
25 |
Correct |
1 ms |
1236 KB |
correct |
26 |
Correct |
2 ms |
1236 KB |
correct |
27 |
Correct |
3 ms |
1236 KB |
correct |
28 |
Correct |
1 ms |
1236 KB |
correct |
29 |
Correct |
2 ms |
1236 KB |
correct |
30 |
Correct |
2 ms |
1236 KB |
correct |
31 |
Correct |
2 ms |
1236 KB |
correct |
32 |
Correct |
2 ms |
1236 KB |
correct |
33 |
Correct |
3 ms |
1236 KB |
correct |
34 |
Correct |
60 ms |
2080 KB |
correct |
35 |
Correct |
52 ms |
2132 KB |
correct |
36 |
Correct |
48 ms |
2004 KB |
correct |
37 |
Correct |
18 ms |
1416 KB |
correct |
38 |
Correct |
52 ms |
2124 KB |
correct |
39 |
Correct |
44 ms |
2004 KB |
correct |
40 |
Correct |
45 ms |
2004 KB |
correct |
41 |
Correct |
55 ms |
2132 KB |
correct |
42 |
Correct |
57 ms |
2132 KB |
correct |
43 |
Correct |
41 ms |
1848 KB |
correct |
44 |
Correct |
36 ms |
1772 KB |
correct |
45 |
Correct |
46 ms |
1748 KB |
correct |
46 |
Correct |
38 ms |
1648 KB |
correct |
47 |
Correct |
28 ms |
1512 KB |
correct |
48 |
Correct |
12 ms |
1364 KB |
correct |
49 |
Correct |
17 ms |
1412 KB |
correct |
50 |
Correct |
27 ms |
1492 KB |
correct |
51 |
Correct |
41 ms |
1748 KB |
correct |
52 |
Correct |
37 ms |
1768 KB |
correct |
53 |
Correct |
35 ms |
1628 KB |
correct |
54 |
Correct |
41 ms |
1836 KB |
correct |
55 |
Correct |
43 ms |
1748 KB |
correct |
56 |
Correct |
47 ms |
1748 KB |
correct |
57 |
Correct |
44 ms |
1748 KB |
correct |
58 |
Correct |
1 ms |
232 KB |
correct |
59 |
Correct |
1 ms |
1236 KB |
correct |
60 |
Correct |
159 ms |
3616 KB |
correct |
61 |
Correct |
254 ms |
4564 KB |
correct |
62 |
Correct |
290 ms |
4568 KB |
correct |
63 |
Correct |
242 ms |
4616 KB |
correct |
64 |
Correct |
166 ms |
4564 KB |
correct |
65 |
Correct |
181 ms |
4564 KB |
correct |
66 |
Correct |
249 ms |
4564 KB |
correct |
67 |
Correct |
275 ms |
4568 KB |
correct |
68 |
Correct |
285 ms |
4580 KB |
correct |
69 |
Correct |
263 ms |
4672 KB |
correct |
70 |
Correct |
1 ms |
1236 KB |
correct |
71 |
Correct |
249 ms |
4568 KB |
correct |
72 |
Correct |
253 ms |
4568 KB |
correct |
73 |
Correct |
0 ms |
212 KB |
correct |
74 |
Correct |
258 ms |
4544 KB |
correct |
75 |
Correct |
250 ms |
5348 KB |
correct |
76 |
Correct |
142 ms |
2964 KB |
correct |
77 |
Correct |
275 ms |
5500 KB |
correct |
78 |
Correct |
256 ms |
5492 KB |
correct |
79 |
Correct |
266 ms |
5652 KB |
correct |
80 |
Correct |
258 ms |
5376 KB |
correct |
81 |
Correct |
185 ms |
4812 KB |
correct |
82 |
Correct |
270 ms |
5444 KB |
correct |
83 |
Correct |
233 ms |
3408 KB |
correct |
84 |
Correct |
215 ms |
4168 KB |
correct |
85 |
Correct |
209 ms |
3588 KB |
correct |
86 |
Correct |
184 ms |
2928 KB |
correct |
87 |
Correct |
168 ms |
2388 KB |
correct |
88 |
Correct |
164 ms |
2232 KB |
correct |
89 |
Correct |
150 ms |
2260 KB |
correct |
90 |
Correct |
136 ms |
2128 KB |
correct |
91 |
Correct |
76 ms |
1472 KB |
correct |
92 |
Correct |
57 ms |
1428 KB |
correct |
93 |
Correct |
202 ms |
3588 KB |
correct |
94 |
Correct |
187 ms |
2940 KB |
correct |
95 |
Correct |
206 ms |
2896 KB |
correct |
96 |
Correct |
150 ms |
2144 KB |
correct |
97 |
Correct |
159 ms |
2264 KB |
correct |
98 |
Correct |
173 ms |
2448 KB |
correct |
99 |
Correct |
166 ms |
2260 KB |
correct |
100 |
Correct |
92 ms |
1616 KB |
correct |
101 |
Correct |
49 ms |
1460 KB |
correct |
102 |
Correct |
218 ms |
3636 KB |
correct |
103 |
Correct |
233 ms |
3508 KB |
correct |
104 |
Correct |
202 ms |
3400 KB |
correct |
105 |
Correct |
205 ms |
3492 KB |
correct |