#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
class DisjointSet {
private:
vector<int> p;
vector<int> sz;
public:
DisjointSet() {}
DisjointSet(int n) {
p.assign(n, 0);
iota(begin(p), end(p), 0);
sz.assign(n, 1);
}
int Find(int x) {
return p[x] == x ? x : p[x] = Find(p[x]);
}
bool Unite(int x, int y) {
x = Find(x), y = Find(y);
if (x == y) return false;
if (sz[x] > sz[y]) swap(x, y);
p[x] = y;
sz[y] += sz[x];
return true;
}
bool SameSet(int x, int y) {
return Find(x) == Find(y);
}
};
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
int m = u.size();
DisjointSet dsu(n);
vector<int> T; // Spanning Tree of G
vector<vector<int>> Tadj(n);
vector<int> Tpar(n, -1);
vector<int> Tdepth(n, 0);
vector<bool> inT(m, false);
vector<int> is_royal(m, -1); // undetermined: -1, not royal: 0, royal: 1
vector<vector<int>> adj(n);
for (int i = 0; i < m; i++) {
adj[u[i]].emplace_back(i);
if (dsu.Unite(u[i], v[i])) {
inT[i] = true;
T.emplace_back(i);
Tadj[u[i]].emplace_back(i);
Tadj[v[i]].emplace_back(i);
}
}
int Troyal = count_common_roads(T);
function<void(int, int)> dfs = [&](int cur, int p) {
for (auto e : Tadj[cur]) {
int nxt = (u[e] == cur ? v[e] : u[e]);
if (nxt != p) {
Tpar[nxt] = e;
Tdepth[nxt] = Tdepth[cur] + 1;
dfs(nxt, cur);
}
}
};
function<void(int)> determine = [&](int edge) {
function<int(vector<int> T_, int, int)> Query =
[&](vector<int> T_, int remove, int add) {
replace(begin(T_), end(T_), remove, add);
return count_common_roads(T_);
};
vector<int> C; // Cycle
int a = u[edge], b = v[edge];
// Get edges of T in the cycle
if (Tdepth[a] > Tdepth[b]) swap(a, b);
while (Tdepth[b] > Tdepth[a]) {
C.emplace_back(Tpar[b]);
b = (u[Tpar[b]] == b ? v[Tpar[b]] : u[Tpar[b]]);
}
while (a != b) {
C.emplace_back(Tpar[a]);
C.emplace_back(Tpar[b]);
a = (u[Tpar[a]] == a ? v[Tpar[a]] : u[Tpar[a]]);
b = (u[Tpar[b]] == b ? v[Tpar[b]] : u[Tpar[b]]);
}
// check if there is an already determined edge
for (auto e : C) {
if (is_royal[edge] == -1 && is_royal[e] != -1) { // there is an already determined edge
int Nroyal = Query(T, e, edge);
if (Nroyal == Troyal) {
is_royal[edge] = is_royal[e];
} else {
is_royal[edge] = is_royal[e] ? 0 : 1;
}
}
}
// all edges are undetermined, check each of them
if (is_royal[edge] == -1) {
vector<int> same;
for (auto e : C) {
int Nroyal = Query(T, e, edge);
if (Nroyal == Troyal) {
same.emplace_back(e);
} else if (Nroyal < Troyal) { // e is a royal road while edge isn't
is_royal[e] = 1;
is_royal[edge] = 0;
break;
} else if (Nroyal > Troyal) { // e isn't a royal road while edge is
is_royal[e] = 0;
is_royal[edge] = 1;
break;
}
}
if (is_royal[edge] == -1) { // royal roads will never form a cycle
is_royal[edge] = 0;
}
// update edges which have the same royality as edge
for (auto e : same) {
is_royal[e] = is_royal[edge];
}
}
// we have determined edge, now to determine other edges in the cycle
for (auto e : C) {
if (is_royal[e] == -1) {
int Nroyal = Query(T, e, edge);
if (Nroyal == Troyal) {
is_royal[e] = is_royal[edge];
} else {
is_royal[e] = is_royal[edge] ? 0 : 1;
}
}
}
for (auto e : C) { // create connected component with determined edges
dsu.Unite(u[e], v[e]);
}
};
function<void(int, vector<int>)> find_royal = [&](int vertex, vector<int> adjacent) {
function<int(vector<int>, vector<int>)> Query =
[&](vector<int> T_, vector<int> E) { // complete the golden set E with edges from T_
DisjointSet d(n);
for (auto e : E) {
d.Unite(u[e], v[e]);
}
int royals_from_T = 0;
for (auto e : T_) {
if (d.Unite(u[e], v[e])) {
royals_from_T += is_royal[e];
E.emplace_back(e);
}
}
return count_common_roads(E) - royals_from_T;
};
int count_royals = Query(T, adjacent);
for (int L = 0; count_royals > 0; count_royals--) {
int lo = L;
int hi = (int) adjacent.size() - 1;
while (lo < hi) {
int mid = (lo + hi) / 2;
vector<int> E(begin(adjacent) + L, begin(adjacent) + mid + 1);
if (Query(T, E) > 0) {
hi = mid;
} else {
lo = mid + 1;
}
}
is_royal[adjacent[lo]] = 1;
L = lo + 1;
}
for (auto e : adjacent) {
if (is_royal[e] == -1) {
is_royal[e] = 0;
}
}
};
dfs(0, -1);
dsu = DisjointSet(n); // determine whether edges from u[i] and v[i] are all determined
for (int i = 0; i < m; i++) {
if (inT[i]) continue; // edge is in T
if (dsu.Unite(u[i], v[i])) {
determine(i); // determine royalities of cycle from u[i] to v[i]
}
}
for (auto e : T) {
if (is_royal[e] == -1) { // if an edge in T is undetermined, that means it is a bridge
is_royal[e] = 1;
}
}
// we have determined all roads in T, determine the remaining royal roads with binary search
for (int i = 0; i < n; i++) {
find_royal(i, adj[i]);
}
vector<int> royal_roads;
for (int i = 0; i < m; i++) {
if (is_royal[i] == 1) {
royal_roads.emplace_back(i);
}
}
return royal_roads;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
correct |
2 |
Correct |
5 ms |
256 KB |
correct |
3 |
Correct |
5 ms |
256 KB |
correct |
4 |
Correct |
4 ms |
256 KB |
correct |
5 |
Correct |
5 ms |
256 KB |
correct |
6 |
Correct |
5 ms |
256 KB |
correct |
7 |
Correct |
4 ms |
256 KB |
correct |
8 |
Correct |
5 ms |
256 KB |
correct |
9 |
Correct |
5 ms |
256 KB |
correct |
10 |
Correct |
4 ms |
256 KB |
correct |
11 |
Correct |
4 ms |
256 KB |
correct |
12 |
Correct |
5 ms |
256 KB |
correct |
13 |
Correct |
5 ms |
256 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
correct |
2 |
Correct |
5 ms |
256 KB |
correct |
3 |
Correct |
5 ms |
256 KB |
correct |
4 |
Correct |
4 ms |
256 KB |
correct |
5 |
Correct |
5 ms |
256 KB |
correct |
6 |
Correct |
5 ms |
256 KB |
correct |
7 |
Correct |
4 ms |
256 KB |
correct |
8 |
Correct |
5 ms |
256 KB |
correct |
9 |
Correct |
5 ms |
256 KB |
correct |
10 |
Correct |
4 ms |
256 KB |
correct |
11 |
Correct |
4 ms |
256 KB |
correct |
12 |
Correct |
5 ms |
256 KB |
correct |
13 |
Correct |
5 ms |
256 KB |
correct |
14 |
Correct |
6 ms |
384 KB |
correct |
15 |
Correct |
6 ms |
384 KB |
correct |
16 |
Correct |
6 ms |
384 KB |
correct |
17 |
Correct |
6 ms |
384 KB |
correct |
18 |
Correct |
5 ms |
384 KB |
correct |
19 |
Correct |
6 ms |
384 KB |
correct |
20 |
Correct |
6 ms |
384 KB |
correct |
21 |
Correct |
6 ms |
384 KB |
correct |
22 |
Correct |
6 ms |
384 KB |
correct |
23 |
Correct |
6 ms |
384 KB |
correct |
24 |
Correct |
6 ms |
384 KB |
correct |
25 |
Correct |
5 ms |
384 KB |
correct |
26 |
Correct |
6 ms |
384 KB |
correct |
27 |
Correct |
6 ms |
384 KB |
correct |
28 |
Correct |
6 ms |
384 KB |
correct |
29 |
Correct |
5 ms |
384 KB |
correct |
30 |
Correct |
6 ms |
384 KB |
correct |
31 |
Correct |
6 ms |
384 KB |
correct |
32 |
Correct |
6 ms |
384 KB |
correct |
33 |
Correct |
6 ms |
372 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
correct |
2 |
Correct |
5 ms |
256 KB |
correct |
3 |
Correct |
5 ms |
256 KB |
correct |
4 |
Correct |
4 ms |
256 KB |
correct |
5 |
Correct |
5 ms |
256 KB |
correct |
6 |
Correct |
5 ms |
256 KB |
correct |
7 |
Correct |
4 ms |
256 KB |
correct |
8 |
Correct |
5 ms |
256 KB |
correct |
9 |
Correct |
5 ms |
256 KB |
correct |
10 |
Correct |
4 ms |
256 KB |
correct |
11 |
Correct |
4 ms |
256 KB |
correct |
12 |
Correct |
5 ms |
256 KB |
correct |
13 |
Correct |
5 ms |
256 KB |
correct |
14 |
Correct |
6 ms |
384 KB |
correct |
15 |
Correct |
6 ms |
384 KB |
correct |
16 |
Correct |
6 ms |
384 KB |
correct |
17 |
Correct |
6 ms |
384 KB |
correct |
18 |
Correct |
5 ms |
384 KB |
correct |
19 |
Correct |
6 ms |
384 KB |
correct |
20 |
Correct |
6 ms |
384 KB |
correct |
21 |
Correct |
6 ms |
384 KB |
correct |
22 |
Correct |
6 ms |
384 KB |
correct |
23 |
Correct |
6 ms |
384 KB |
correct |
24 |
Correct |
6 ms |
384 KB |
correct |
25 |
Correct |
5 ms |
384 KB |
correct |
26 |
Correct |
6 ms |
384 KB |
correct |
27 |
Correct |
6 ms |
384 KB |
correct |
28 |
Correct |
6 ms |
384 KB |
correct |
29 |
Correct |
5 ms |
384 KB |
correct |
30 |
Correct |
6 ms |
384 KB |
correct |
31 |
Correct |
6 ms |
384 KB |
correct |
32 |
Correct |
6 ms |
384 KB |
correct |
33 |
Correct |
6 ms |
372 KB |
correct |
34 |
Correct |
47 ms |
1152 KB |
correct |
35 |
Correct |
47 ms |
1152 KB |
correct |
36 |
Correct |
40 ms |
896 KB |
correct |
37 |
Correct |
18 ms |
384 KB |
correct |
38 |
Correct |
47 ms |
1276 KB |
correct |
39 |
Correct |
43 ms |
1024 KB |
correct |
40 |
Correct |
39 ms |
896 KB |
correct |
41 |
Correct |
43 ms |
1152 KB |
correct |
42 |
Correct |
45 ms |
1152 KB |
correct |
43 |
Correct |
38 ms |
896 KB |
correct |
44 |
Correct |
33 ms |
768 KB |
correct |
45 |
Correct |
36 ms |
812 KB |
correct |
46 |
Correct |
34 ms |
640 KB |
correct |
47 |
Correct |
27 ms |
512 KB |
correct |
48 |
Correct |
11 ms |
384 KB |
correct |
49 |
Correct |
17 ms |
384 KB |
correct |
50 |
Correct |
31 ms |
512 KB |
correct |
51 |
Correct |
37 ms |
768 KB |
correct |
52 |
Correct |
34 ms |
768 KB |
correct |
53 |
Correct |
33 ms |
640 KB |
correct |
54 |
Correct |
37 ms |
896 KB |
correct |
55 |
Correct |
35 ms |
768 KB |
correct |
56 |
Correct |
33 ms |
768 KB |
correct |
57 |
Correct |
33 ms |
768 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
correct |
2 |
Correct |
5 ms |
384 KB |
correct |
3 |
Correct |
132 ms |
2688 KB |
correct |
4 |
Correct |
211 ms |
3584 KB |
correct |
5 |
Correct |
209 ms |
3704 KB |
correct |
6 |
Correct |
199 ms |
3704 KB |
correct |
7 |
Correct |
194 ms |
3832 KB |
correct |
8 |
Correct |
204 ms |
3704 KB |
correct |
9 |
Correct |
207 ms |
3704 KB |
correct |
10 |
Correct |
229 ms |
3704 KB |
correct |
11 |
Correct |
216 ms |
3832 KB |
correct |
12 |
Correct |
218 ms |
3832 KB |
correct |
13 |
Correct |
5 ms |
384 KB |
correct |
14 |
Correct |
199 ms |
3756 KB |
correct |
15 |
Correct |
230 ms |
3708 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
256 KB |
correct |
2 |
Correct |
5 ms |
256 KB |
correct |
3 |
Correct |
5 ms |
256 KB |
correct |
4 |
Correct |
4 ms |
256 KB |
correct |
5 |
Correct |
5 ms |
256 KB |
correct |
6 |
Correct |
5 ms |
256 KB |
correct |
7 |
Correct |
4 ms |
256 KB |
correct |
8 |
Correct |
5 ms |
256 KB |
correct |
9 |
Correct |
5 ms |
256 KB |
correct |
10 |
Correct |
4 ms |
256 KB |
correct |
11 |
Correct |
4 ms |
256 KB |
correct |
12 |
Correct |
5 ms |
256 KB |
correct |
13 |
Correct |
5 ms |
256 KB |
correct |
14 |
Correct |
6 ms |
384 KB |
correct |
15 |
Correct |
6 ms |
384 KB |
correct |
16 |
Correct |
6 ms |
384 KB |
correct |
17 |
Correct |
6 ms |
384 KB |
correct |
18 |
Correct |
5 ms |
384 KB |
correct |
19 |
Correct |
6 ms |
384 KB |
correct |
20 |
Correct |
6 ms |
384 KB |
correct |
21 |
Correct |
6 ms |
384 KB |
correct |
22 |
Correct |
6 ms |
384 KB |
correct |
23 |
Correct |
6 ms |
384 KB |
correct |
24 |
Correct |
6 ms |
384 KB |
correct |
25 |
Correct |
5 ms |
384 KB |
correct |
26 |
Correct |
6 ms |
384 KB |
correct |
27 |
Correct |
6 ms |
384 KB |
correct |
28 |
Correct |
6 ms |
384 KB |
correct |
29 |
Correct |
5 ms |
384 KB |
correct |
30 |
Correct |
6 ms |
384 KB |
correct |
31 |
Correct |
6 ms |
384 KB |
correct |
32 |
Correct |
6 ms |
384 KB |
correct |
33 |
Correct |
6 ms |
372 KB |
correct |
34 |
Correct |
47 ms |
1152 KB |
correct |
35 |
Correct |
47 ms |
1152 KB |
correct |
36 |
Correct |
40 ms |
896 KB |
correct |
37 |
Correct |
18 ms |
384 KB |
correct |
38 |
Correct |
47 ms |
1276 KB |
correct |
39 |
Correct |
43 ms |
1024 KB |
correct |
40 |
Correct |
39 ms |
896 KB |
correct |
41 |
Correct |
43 ms |
1152 KB |
correct |
42 |
Correct |
45 ms |
1152 KB |
correct |
43 |
Correct |
38 ms |
896 KB |
correct |
44 |
Correct |
33 ms |
768 KB |
correct |
45 |
Correct |
36 ms |
812 KB |
correct |
46 |
Correct |
34 ms |
640 KB |
correct |
47 |
Correct |
27 ms |
512 KB |
correct |
48 |
Correct |
11 ms |
384 KB |
correct |
49 |
Correct |
17 ms |
384 KB |
correct |
50 |
Correct |
31 ms |
512 KB |
correct |
51 |
Correct |
37 ms |
768 KB |
correct |
52 |
Correct |
34 ms |
768 KB |
correct |
53 |
Correct |
33 ms |
640 KB |
correct |
54 |
Correct |
37 ms |
896 KB |
correct |
55 |
Correct |
35 ms |
768 KB |
correct |
56 |
Correct |
33 ms |
768 KB |
correct |
57 |
Correct |
33 ms |
768 KB |
correct |
58 |
Correct |
5 ms |
256 KB |
correct |
59 |
Correct |
5 ms |
384 KB |
correct |
60 |
Correct |
132 ms |
2688 KB |
correct |
61 |
Correct |
211 ms |
3584 KB |
correct |
62 |
Correct |
209 ms |
3704 KB |
correct |
63 |
Correct |
199 ms |
3704 KB |
correct |
64 |
Correct |
194 ms |
3832 KB |
correct |
65 |
Correct |
204 ms |
3704 KB |
correct |
66 |
Correct |
207 ms |
3704 KB |
correct |
67 |
Correct |
229 ms |
3704 KB |
correct |
68 |
Correct |
216 ms |
3832 KB |
correct |
69 |
Correct |
218 ms |
3832 KB |
correct |
70 |
Correct |
5 ms |
384 KB |
correct |
71 |
Correct |
199 ms |
3756 KB |
correct |
72 |
Correct |
230 ms |
3708 KB |
correct |
73 |
Correct |
4 ms |
256 KB |
correct |
74 |
Correct |
214 ms |
3752 KB |
correct |
75 |
Correct |
218 ms |
3576 KB |
correct |
76 |
Correct |
113 ms |
1536 KB |
correct |
77 |
Correct |
229 ms |
3712 KB |
correct |
78 |
Correct |
216 ms |
3704 KB |
correct |
79 |
Correct |
229 ms |
3808 KB |
correct |
80 |
Correct |
218 ms |
3448 KB |
correct |
81 |
Correct |
196 ms |
3072 KB |
correct |
82 |
Correct |
224 ms |
3448 KB |
correct |
83 |
Correct |
174 ms |
2164 KB |
correct |
84 |
Correct |
170 ms |
2552 KB |
correct |
85 |
Correct |
161 ms |
2176 KB |
correct |
86 |
Correct |
140 ms |
1664 KB |
correct |
87 |
Correct |
130 ms |
1400 KB |
correct |
88 |
Correct |
115 ms |
1152 KB |
correct |
89 |
Correct |
114 ms |
1144 KB |
correct |
90 |
Correct |
109 ms |
1024 KB |
correct |
91 |
Correct |
53 ms |
512 KB |
correct |
92 |
Correct |
33 ms |
384 KB |
correct |
93 |
Correct |
164 ms |
2264 KB |
correct |
94 |
Correct |
142 ms |
1664 KB |
correct |
95 |
Correct |
143 ms |
1536 KB |
correct |
96 |
Correct |
114 ms |
1096 KB |
correct |
97 |
Correct |
130 ms |
1152 KB |
correct |
98 |
Correct |
129 ms |
1408 KB |
correct |
99 |
Correct |
117 ms |
1152 KB |
correct |
100 |
Correct |
81 ms |
640 KB |
correct |
101 |
Correct |
35 ms |
384 KB |
correct |
102 |
Correct |
145 ms |
2176 KB |
correct |
103 |
Correct |
144 ms |
2176 KB |
correct |
104 |
Correct |
150 ms |
2160 KB |
correct |
105 |
Correct |
145 ms |
2296 KB |
correct |