#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n, seen[500], usede[125000], in_set[500], royal[125000], cnt;
vector<pii> adj[500];
vector<int> u,v;
struct DSU {
vector<int> dsu;
DSU (int n) {
dsu.resize(n);
iota(dsu.begin(), dsu.end(), 0);
}
int f(int x) {return dsu[x] == x ? x : dsu[x] = f(dsu[x]);}
bool merge(int a, int b) {
a = f(a), b = f(b);
if (a != b) {
dsu[b] = a;
return 1;
}
return 0;
}
};
void dfs(int u, int ma, int ex, vector<int> &r, DSU &dsu) {
seen[u] = cnt;
for (pii v: adj[u]) {
if (u == ma && dsu.f(v.first) == ex) continue;
if (dsu.f(u) == ex && v.first == ma) continue;
if (seen[v.first] != cnt) {
r.push_back(v.second);
dfs(v.first, ma, ex, r, dsu);
}
}
}
void dfs2(int u, vector<int> &r) {
seen[u] = cnt;
for (pii v: adj[u]) {
if (seen[v.first] != cnt) {
if (!in_set[v.first]) r.push_back(v.second);
dfs2(v.first, r);
}
}
}
int query(int offset, vector<int> &edges, vector<int> &span) {
vector<int> res = edges;
DSU dsu(n);
for (int e: edges) {
dsu.merge(u[e], v[e]);
}
for (int e: span) {
if (dsu.merge(u[e], v[e])) {
res.push_back(e);
offset += royal[e];
}
}
return count_common_roads(res) - offset;
}
void dnc(int cnt, int offset, vector<int> &edges, vector<int> &span) {
if (cnt == 0) return;
if (cnt == edges.size()) {
for (int e: edges) royal[e] = 1;
return;
}
int m = edges.size() / 2;
vector<int> lft, rht;
for (int e: edges) {
(lft.size() < m ? lft : rht).push_back(e);
}
int res = query(offset, lft, span);
dnc(res, offset, lft, span);
dnc(cnt-res, offset, rht, span);
}
std::vector<int> find_roads(int _n, std::vector<int> _u, std::vector<int> _v) {
n = _n;
u = _u;
v = _v;
vector<int> ans, stk{0}, span;
in_set[0] = 1;
for (int i = 0; i < u.size(); ++i) {
adj[u[i]].push_back({v[i], i});
adj[v[i]].push_back({u[i], i});
}
while (stk.size()) {
int i = stk.back(); stk.pop_back();
vector<int> nxt;
DSU dsu(n);
for (int j = 0; j < u.size(); ++j) {
if (u[j] != i && v[j] != i) dsu.merge(u[j], v[j]);
}
vector<vector<pii>> es(n);
for (pii e: adj[i]) es[dsu.f(e.first)].push_back(e);
vector<int> vals = dsu.dsu;
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
for (int ex: vals) if (ex != i) {
vector<int> r;
cnt++;
for (int j = 0; j < n; ++j) if (j != i && seen[j] != cnt) {
dfs(j, i, ex, r, dsu);
}
bool seen_royal = 0;
vector<pii> res;
for (pii e: es[ex]) {
if (!usede[e.second] || (!seen_royal && royal[e.second])) {
if (!in_set[e.first]) {
in_set[e.first] = 1;
nxt.push_back(e.first);
span.push_back(e.second);
}
usede[e.second] = 1;
if (royal[e.second]) seen_royal = 1;
r.push_back(e.second);
res.push_back({count_common_roads(r), e.second});
r.pop_back();
}
}
sort(res.begin(), res.end());
int bst = res.back().first;
while (res.size() && res.back().first == bst) {
if (!royal[res.back().second]) {
royal[res.back().second] = 1;
}
res.pop_back();
}
}
vector<int> total_span = span;
cnt++;
dfs2(0, total_span);
int span_rcnt = 0;
for (int e: span) span_rcnt += royal[e];
for (int nx: nxt) {
vector<int> edges;
stk.push_back(nx);
for (pii e: adj[nx]) if (in_set[e.first] && !usede[e.second]) {
edges.push_back(e.second);
usede[e.second] = 1;
}
int offset = count_common_roads(total_span) - span_rcnt;
dnc(query(offset, edges, total_span), offset, edges, total_span);
}
}
for (int i = 0; i < u.size(); ++i) if (royal[i]) ans.push_back(i);
return ans;
}
Compilation message
simurgh.cpp: In function 'void dnc(int, int, std::vector<int>&, std::vector<int>&)':
simurgh.cpp:66:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | if (cnt == edges.size()) {
| ~~~~^~~~~~~~~~~~~~~
simurgh.cpp:73:15: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
73 | (lft.size() < m ? lft : rht).push_back(e);
| ~~~~~~~~~~~^~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:86:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int i = 0; i < u.size(); ++i) {
| ~~^~~~~~~~~~
simurgh.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int j = 0; j < u.size(); ++j) {
| ~~^~~~~~~~~~
simurgh.cpp:149:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | for (int i = 0; i < u.size(); ++i) if (royal[i]) ans.push_back(i);
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
312 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
312 KB |
correct |
8 |
Correct |
1 ms |
316 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
296 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
332 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
312 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
312 KB |
correct |
8 |
Correct |
1 ms |
316 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
296 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
332 KB |
correct |
14 |
Correct |
4 ms |
332 KB |
correct |
15 |
Correct |
3 ms |
408 KB |
correct |
16 |
Correct |
4 ms |
332 KB |
correct |
17 |
Correct |
3 ms |
332 KB |
correct |
18 |
Correct |
2 ms |
332 KB |
correct |
19 |
Correct |
3 ms |
332 KB |
correct |
20 |
Correct |
3 ms |
332 KB |
correct |
21 |
Correct |
3 ms |
332 KB |
correct |
22 |
Correct |
3 ms |
332 KB |
correct |
23 |
Correct |
3 ms |
332 KB |
correct |
24 |
Correct |
3 ms |
312 KB |
correct |
25 |
Correct |
2 ms |
332 KB |
correct |
26 |
Correct |
2 ms |
312 KB |
correct |
27 |
Correct |
3 ms |
332 KB |
correct |
28 |
Correct |
2 ms |
332 KB |
correct |
29 |
Correct |
2 ms |
332 KB |
correct |
30 |
Correct |
3 ms |
316 KB |
correct |
31 |
Correct |
3 ms |
332 KB |
correct |
32 |
Correct |
3 ms |
332 KB |
correct |
33 |
Correct |
3 ms |
332 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
312 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
312 KB |
correct |
8 |
Correct |
1 ms |
316 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
296 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
332 KB |
correct |
14 |
Correct |
4 ms |
332 KB |
correct |
15 |
Correct |
3 ms |
408 KB |
correct |
16 |
Correct |
4 ms |
332 KB |
correct |
17 |
Correct |
3 ms |
332 KB |
correct |
18 |
Correct |
2 ms |
332 KB |
correct |
19 |
Correct |
3 ms |
332 KB |
correct |
20 |
Correct |
3 ms |
332 KB |
correct |
21 |
Correct |
3 ms |
332 KB |
correct |
22 |
Correct |
3 ms |
332 KB |
correct |
23 |
Correct |
3 ms |
332 KB |
correct |
24 |
Correct |
3 ms |
312 KB |
correct |
25 |
Correct |
2 ms |
332 KB |
correct |
26 |
Correct |
2 ms |
312 KB |
correct |
27 |
Correct |
3 ms |
332 KB |
correct |
28 |
Correct |
2 ms |
332 KB |
correct |
29 |
Correct |
2 ms |
332 KB |
correct |
30 |
Correct |
3 ms |
316 KB |
correct |
31 |
Correct |
3 ms |
332 KB |
correct |
32 |
Correct |
3 ms |
332 KB |
correct |
33 |
Correct |
3 ms |
332 KB |
correct |
34 |
Correct |
173 ms |
2032 KB |
correct |
35 |
Correct |
169 ms |
1992 KB |
correct |
36 |
Correct |
134 ms |
1612 KB |
correct |
37 |
Correct |
31 ms |
472 KB |
correct |
38 |
Correct |
174 ms |
2020 KB |
correct |
39 |
Correct |
162 ms |
1860 KB |
correct |
40 |
Correct |
133 ms |
1664 KB |
correct |
41 |
Correct |
171 ms |
1976 KB |
correct |
42 |
Correct |
163 ms |
1996 KB |
correct |
43 |
Correct |
98 ms |
1356 KB |
correct |
44 |
Correct |
84 ms |
1064 KB |
correct |
45 |
Correct |
93 ms |
1068 KB |
correct |
46 |
Correct |
76 ms |
980 KB |
correct |
47 |
Correct |
43 ms |
680 KB |
correct |
48 |
Correct |
78 ms |
376 KB |
correct |
49 |
Correct |
47 ms |
460 KB |
correct |
50 |
Correct |
42 ms |
660 KB |
correct |
51 |
Correct |
94 ms |
1164 KB |
correct |
52 |
Correct |
82 ms |
972 KB |
correct |
53 |
Correct |
75 ms |
972 KB |
correct |
54 |
Correct |
97 ms |
1356 KB |
correct |
55 |
Correct |
93 ms |
1104 KB |
correct |
56 |
Correct |
94 ms |
1220 KB |
correct |
57 |
Correct |
94 ms |
1100 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
332 KB |
correct |
3 |
Correct |
728 ms |
5232 KB |
correct |
4 |
Correct |
1389 ms |
7612 KB |
correct |
5 |
Correct |
1412 ms |
7516 KB |
correct |
6 |
Correct |
1302 ms |
7548 KB |
correct |
7 |
Correct |
1329 ms |
7540 KB |
correct |
8 |
Correct |
1345 ms |
7508 KB |
correct |
9 |
Correct |
1402 ms |
7524 KB |
correct |
10 |
Correct |
1420 ms |
7492 KB |
correct |
11 |
Correct |
1399 ms |
7620 KB |
correct |
12 |
Correct |
1414 ms |
7640 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
1387 ms |
7520 KB |
correct |
15 |
Correct |
1427 ms |
7512 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
204 KB |
correct |
4 |
Correct |
1 ms |
312 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
312 KB |
correct |
8 |
Correct |
1 ms |
316 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
296 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
332 KB |
correct |
14 |
Correct |
4 ms |
332 KB |
correct |
15 |
Correct |
3 ms |
408 KB |
correct |
16 |
Correct |
4 ms |
332 KB |
correct |
17 |
Correct |
3 ms |
332 KB |
correct |
18 |
Correct |
2 ms |
332 KB |
correct |
19 |
Correct |
3 ms |
332 KB |
correct |
20 |
Correct |
3 ms |
332 KB |
correct |
21 |
Correct |
3 ms |
332 KB |
correct |
22 |
Correct |
3 ms |
332 KB |
correct |
23 |
Correct |
3 ms |
332 KB |
correct |
24 |
Correct |
3 ms |
312 KB |
correct |
25 |
Correct |
2 ms |
332 KB |
correct |
26 |
Correct |
2 ms |
312 KB |
correct |
27 |
Correct |
3 ms |
332 KB |
correct |
28 |
Correct |
2 ms |
332 KB |
correct |
29 |
Correct |
2 ms |
332 KB |
correct |
30 |
Correct |
3 ms |
316 KB |
correct |
31 |
Correct |
3 ms |
332 KB |
correct |
32 |
Correct |
3 ms |
332 KB |
correct |
33 |
Correct |
3 ms |
332 KB |
correct |
34 |
Correct |
173 ms |
2032 KB |
correct |
35 |
Correct |
169 ms |
1992 KB |
correct |
36 |
Correct |
134 ms |
1612 KB |
correct |
37 |
Correct |
31 ms |
472 KB |
correct |
38 |
Correct |
174 ms |
2020 KB |
correct |
39 |
Correct |
162 ms |
1860 KB |
correct |
40 |
Correct |
133 ms |
1664 KB |
correct |
41 |
Correct |
171 ms |
1976 KB |
correct |
42 |
Correct |
163 ms |
1996 KB |
correct |
43 |
Correct |
98 ms |
1356 KB |
correct |
44 |
Correct |
84 ms |
1064 KB |
correct |
45 |
Correct |
93 ms |
1068 KB |
correct |
46 |
Correct |
76 ms |
980 KB |
correct |
47 |
Correct |
43 ms |
680 KB |
correct |
48 |
Correct |
78 ms |
376 KB |
correct |
49 |
Correct |
47 ms |
460 KB |
correct |
50 |
Correct |
42 ms |
660 KB |
correct |
51 |
Correct |
94 ms |
1164 KB |
correct |
52 |
Correct |
82 ms |
972 KB |
correct |
53 |
Correct |
75 ms |
972 KB |
correct |
54 |
Correct |
97 ms |
1356 KB |
correct |
55 |
Correct |
93 ms |
1104 KB |
correct |
56 |
Correct |
94 ms |
1220 KB |
correct |
57 |
Correct |
94 ms |
1100 KB |
correct |
58 |
Correct |
1 ms |
204 KB |
correct |
59 |
Correct |
1 ms |
332 KB |
correct |
60 |
Correct |
728 ms |
5232 KB |
correct |
61 |
Correct |
1389 ms |
7612 KB |
correct |
62 |
Correct |
1412 ms |
7516 KB |
correct |
63 |
Correct |
1302 ms |
7548 KB |
correct |
64 |
Correct |
1329 ms |
7540 KB |
correct |
65 |
Correct |
1345 ms |
7508 KB |
correct |
66 |
Correct |
1402 ms |
7524 KB |
correct |
67 |
Correct |
1420 ms |
7492 KB |
correct |
68 |
Correct |
1399 ms |
7620 KB |
correct |
69 |
Correct |
1414 ms |
7640 KB |
correct |
70 |
Correct |
1 ms |
204 KB |
correct |
71 |
Correct |
1387 ms |
7520 KB |
correct |
72 |
Correct |
1427 ms |
7512 KB |
correct |
73 |
Correct |
1 ms |
204 KB |
correct |
74 |
Correct |
1404 ms |
7512 KB |
correct |
75 |
Correct |
1367 ms |
7516 KB |
correct |
76 |
Correct |
463 ms |
3140 KB |
correct |
77 |
Correct |
1388 ms |
7544 KB |
correct |
78 |
Correct |
1421 ms |
7604 KB |
correct |
79 |
Correct |
1394 ms |
7576 KB |
correct |
80 |
Correct |
1347 ms |
7368 KB |
correct |
81 |
Correct |
1139 ms |
6656 KB |
correct |
82 |
Correct |
1402 ms |
7404 KB |
correct |
83 |
Correct |
764 ms |
4260 KB |
correct |
84 |
Correct |
835 ms |
4800 KB |
correct |
85 |
Correct |
751 ms |
4540 KB |
correct |
86 |
Correct |
563 ms |
3140 KB |
correct |
87 |
Correct |
429 ms |
2648 KB |
correct |
88 |
Correct |
350 ms |
1896 KB |
correct |
89 |
Correct |
332 ms |
1860 KB |
correct |
90 |
Correct |
492 ms |
1660 KB |
correct |
91 |
Correct |
329 ms |
584 KB |
correct |
92 |
Correct |
766 ms |
544 KB |
correct |
93 |
Correct |
763 ms |
4548 KB |
correct |
94 |
Correct |
546 ms |
3020 KB |
correct |
95 |
Correct |
528 ms |
3068 KB |
correct |
96 |
Correct |
304 ms |
1724 KB |
correct |
97 |
Correct |
344 ms |
2116 KB |
correct |
98 |
Correct |
414 ms |
2548 KB |
correct |
99 |
Correct |
361 ms |
1884 KB |
correct |
100 |
Correct |
284 ms |
896 KB |
correct |
101 |
Correct |
690 ms |
492 KB |
correct |
102 |
Correct |
727 ms |
3908 KB |
correct |
103 |
Correct |
774 ms |
3808 KB |
correct |
104 |
Correct |
727 ms |
3768 KB |
correct |
105 |
Correct |
712 ms |
3708 KB |
correct |