#pragma GCC optimize ("O3")
#include "simurgh.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct DSU {
int n, components;
vector<int>par;
DSU(int _n) {
n = _n;
components = n;
par.assign(n, -1);
}
int Find(int u) {
return (par[u] < 0 ? u : (par[u] = Find(par[u])));
}
bool Union(int u, int v) {
u = Find(u);
v = Find(v);
if (u != v) {
if (par[u] > par[v]) {
swap(u, v);
}
par[u] += par[v];
par[v] = u;
--components;
return true;
} else {
return false;
}
}
};
/// helperTree (construct any tree and find its edge types (royal or not))
/// findDegree (find degree for each node)
/// binarySearch (find edges connected to leaf nodes using BS)
bool cmp(array<int, 3>er1, array<int, 3>er2) {
if (er1[0] * er2[1] == er2[0] * er1[1]) {
return er1[0] > er2[0];
} else {
return er1[0] * er2[1] > er2[0] * er1[1];
}
}
vpii findHelperTree(int n, int m, vi &u, vi &v, vvi &g, vi &isRoyal) {
DSU helperTreeDSU(n);
vector<pii>helperTree;
vector<array<int, 3>>edgeRatio(n);
for (int i = 0; i < n; ++i) {
edgeRatio[i] = {size(g[i]), size(g[i]), i};
}
sort(all(edgeRatio), cmp);
for (int itr = 0; itr < n; ++itr) {
int i = edgeRatio[0][2];
DSU dsu(n);
vi r;
for (int j = 0; j < m; ++j) {
if (u[j] != i && v[j] != i && dsu.Union(u[j], v[j])) {
r.eb(j);
}
}
vi roots;
for (int j = 0; j < n; ++j) {
if (j != i && dsu.par[j] < 0) {
roots.eb(j);
}
}
vvi group(n);
for (int j : g[i]) {
int other = u[j] + v[j] - i;
group[dsu.Find(other)].eb(j);
}
for (int j = 0; j < dsu.components - 1; ++j) {
for (int k = 0; k < dsu.components - 1; ++k) {
if (k != j) {
r.eb(group[roots[k]][0]);
}
}
vi royals;
int maxVal = -1;
for (int k : group[roots[j]]) {
if (isRoyal[k] == -1) {
r.eb(k);
royals.eb(count_common_roads(r));
r.pop_back();
} else if (isRoyal[k] == 1) {
if (maxVal == -1) {
r.eb(k);
royals.eb(count_common_roads(r));
r.pop_back();
maxVal = royals.back();
} else {
royals.eb(maxVal);
}
} else {
if (maxVal == -1) {
r.eb(k);
royals.eb(count_common_roads(r));
r.pop_back();
maxVal = royals.back() + 1;
} else {
royals.eb(maxVal - 1);
}
}
}
maxVal = (*max_element(all(royals)));
for (int k = 0; k < size(group[roots[j]]); ++k) {
int id = group[roots[j]][k];
if (royals[k] == maxVal) {
isRoyal[id] = 1;
} else {
isRoyal[id] = 0;
}
if (helperTreeDSU.Union(u[id], v[id])) {
helperTree.eb(mpr(id, isRoyal[id]));
if (helperTreeDSU.components == 1) return helperTree;
}
}
for (int k = 0; k < dsu.components - 1; ++k) {
if (k != j) {
r.pop_back();
}
}
}
vector<set<int>>subTrees(n);
for (int j = 0; j < m; ++j) {
if (helperTreeDSU.Find(u[j]) != helperTreeDSU.Find(v[j])) {
subTrees[u[j]].insert(helperTreeDSU.Find(v[j]));
subTrees[v[j]].insert(helperTreeDSU.Find(u[j]));
}
}
for (int j = 0; j < n; ++j) {
edgeRatio[j] = {size(subTrees[j]), size(g[j]), j};
}
sort(all(edgeRatio), cmp);
}
}
vi findDegrees(int n, int m, vpii &helperTree, vi &u, vi &v, vvi &g) {
vi degree(n, 0);
for (int i = 0; i < n; ++i) {
DSU dsu(n);
vi r;
for (int j : g[i]) {
dsu.Union(u[j], v[j]);
r.eb(j);
}
for (auto [j, isRoyal] : helperTree) {
if (dsu.Union(u[j], v[j])) {
r.eb(j);
degree[i] -= isRoyal;
}
}
degree[i] += count_common_roads(r);
}
return degree;
}
int findRoyal(int n, vpii &helperTree, vi &edges, vi &u, vi &v) {
int low = 0, high = size(edges) - 1;
int mid;
while (low < high) {
mid = (low + high) >> 1;
DSU dsu(n);
vi r;
int royals = 0;
for (int i = low; i <= mid; ++i) {
r.eb(edges[i]);
dsu.Union(u[edges[i]], v[edges[i]]);
}
for (auto [i, isRoyal] : helperTree) {
if (dsu.Union(u[i], v[i])) {
r.eb(i);
royals -= isRoyal;
}
}
royals += count_common_roads(r);
if (royals) {
high = mid;
} else {
low = mid + 1;
}
}
return edges[low];
}
vi find_roads(int n, vi u, vi v) {
int m = size(u);
vvi g(n);
for (int i = 0; i < m; ++i) {
g[u[i]].eb(i);
g[v[i]].eb(i);
}
vi isRoyal(m, -1);
vpii helperTree = findHelperTree(n, m, u, v, g, isRoyal);
vi degree = findDegrees(n, m, helperTree, u, v, g);
queue<int>q;
for (int i = 0; i < n; ++i) {
if (degree[i] == 1) {
q.push(i);
}
}
vector<int>res;
for (int i = 1; i < n; ++i) {
int node = q.front();
q.pop();
vi edges;
int id = -1;
for (int i : g[node]) {
if (isRoyal[i] == -1) {
edges.eb(i);
} else if (isRoyal[i] == 1) {
id = i;
break;
}
}
if (id == -1) {
id = findRoyal(n, helperTree, edges, u, v);
}
for (int i : edges) {
isRoyal[i] = 0;
}
isRoyal[id] = 2;
res.eb(id);
int other = (u[id] ^ v[id] ^ node);
--degree[other];
if (degree[other] == 1) {
q.push(other);
}
}
return res;
}
Compilation message
simurgh.cpp: In function 'std::vector<std::pair<int, int> > findHelperTree(int, int, std::vector<int>&, std::vector<int>&, std::vector<std::vector<int> >&, std::vector<int>&)':
simurgh.cpp:67:22: warning: control reaches end of non-void function [-Wreturn-type]
67 | DSU helperTreeDSU(n);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
304 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
1 ms |
212 KB |
correct |
4 |
Correct |
0 ms |
212 KB |
correct |
5 |
Correct |
0 ms |
212 KB |
correct |
6 |
Correct |
1 ms |
212 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
212 KB |
correct |
9 |
Correct |
1 ms |
212 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
304 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
304 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
1 ms |
212 KB |
correct |
4 |
Correct |
0 ms |
212 KB |
correct |
5 |
Correct |
0 ms |
212 KB |
correct |
6 |
Correct |
1 ms |
212 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
212 KB |
correct |
9 |
Correct |
1 ms |
212 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
304 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
1 ms |
340 KB |
correct |
15 |
Correct |
1 ms |
308 KB |
correct |
16 |
Correct |
1 ms |
340 KB |
correct |
17 |
Correct |
1 ms |
308 KB |
correct |
18 |
Correct |
1 ms |
340 KB |
correct |
19 |
Correct |
2 ms |
340 KB |
correct |
20 |
Correct |
1 ms |
340 KB |
correct |
21 |
Correct |
1 ms |
340 KB |
correct |
22 |
Correct |
1 ms |
340 KB |
correct |
23 |
Correct |
1 ms |
304 KB |
correct |
24 |
Correct |
1 ms |
300 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
340 KB |
correct |
27 |
Correct |
1 ms |
340 KB |
correct |
28 |
Correct |
1 ms |
212 KB |
correct |
29 |
Correct |
1 ms |
212 KB |
correct |
30 |
Correct |
2 ms |
340 KB |
correct |
31 |
Correct |
2 ms |
340 KB |
correct |
32 |
Correct |
1 ms |
340 KB |
correct |
33 |
Correct |
1 ms |
340 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
304 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
1 ms |
212 KB |
correct |
4 |
Correct |
0 ms |
212 KB |
correct |
5 |
Correct |
0 ms |
212 KB |
correct |
6 |
Correct |
1 ms |
212 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
212 KB |
correct |
9 |
Correct |
1 ms |
212 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
304 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
1 ms |
340 KB |
correct |
15 |
Correct |
1 ms |
308 KB |
correct |
16 |
Correct |
1 ms |
340 KB |
correct |
17 |
Correct |
1 ms |
308 KB |
correct |
18 |
Correct |
1 ms |
340 KB |
correct |
19 |
Correct |
2 ms |
340 KB |
correct |
20 |
Correct |
1 ms |
340 KB |
correct |
21 |
Correct |
1 ms |
340 KB |
correct |
22 |
Correct |
1 ms |
340 KB |
correct |
23 |
Correct |
1 ms |
304 KB |
correct |
24 |
Correct |
1 ms |
300 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
340 KB |
correct |
27 |
Correct |
1 ms |
340 KB |
correct |
28 |
Correct |
1 ms |
212 KB |
correct |
29 |
Correct |
1 ms |
212 KB |
correct |
30 |
Correct |
2 ms |
340 KB |
correct |
31 |
Correct |
2 ms |
340 KB |
correct |
32 |
Correct |
1 ms |
340 KB |
correct |
33 |
Correct |
1 ms |
340 KB |
correct |
34 |
Correct |
25 ms |
1332 KB |
correct |
35 |
Correct |
27 ms |
1268 KB |
correct |
36 |
Correct |
28 ms |
1556 KB |
correct |
37 |
Correct |
12 ms |
496 KB |
correct |
38 |
Correct |
27 ms |
1372 KB |
correct |
39 |
Correct |
27 ms |
1424 KB |
correct |
40 |
Correct |
29 ms |
1492 KB |
correct |
41 |
Correct |
26 ms |
1364 KB |
correct |
42 |
Correct |
27 ms |
1364 KB |
correct |
43 |
Correct |
22 ms |
1320 KB |
correct |
44 |
Correct |
21 ms |
1236 KB |
correct |
45 |
Correct |
23 ms |
1496 KB |
correct |
46 |
Correct |
20 ms |
1236 KB |
correct |
47 |
Correct |
17 ms |
892 KB |
correct |
48 |
Correct |
7 ms |
308 KB |
correct |
49 |
Correct |
11 ms |
468 KB |
correct |
50 |
Correct |
17 ms |
876 KB |
correct |
51 |
Correct |
23 ms |
1436 KB |
correct |
52 |
Correct |
22 ms |
1364 KB |
correct |
53 |
Correct |
20 ms |
1240 KB |
correct |
54 |
Correct |
22 ms |
1236 KB |
correct |
55 |
Correct |
23 ms |
1492 KB |
correct |
56 |
Correct |
23 ms |
1492 KB |
correct |
57 |
Correct |
25 ms |
1468 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
84 ms |
3432 KB |
correct |
4 |
Correct |
123 ms |
4812 KB |
correct |
5 |
Correct |
128 ms |
4888 KB |
correct |
6 |
Correct |
131 ms |
4884 KB |
correct |
7 |
Correct |
140 ms |
4980 KB |
correct |
8 |
Correct |
123 ms |
4864 KB |
correct |
9 |
Correct |
124 ms |
4792 KB |
correct |
10 |
Correct |
126 ms |
4884 KB |
correct |
11 |
Correct |
144 ms |
4812 KB |
correct |
12 |
Correct |
126 ms |
4876 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
134 ms |
4852 KB |
correct |
15 |
Correct |
136 ms |
4980 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
304 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
1 ms |
212 KB |
correct |
4 |
Correct |
0 ms |
212 KB |
correct |
5 |
Correct |
0 ms |
212 KB |
correct |
6 |
Correct |
1 ms |
212 KB |
correct |
7 |
Correct |
1 ms |
212 KB |
correct |
8 |
Correct |
1 ms |
212 KB |
correct |
9 |
Correct |
1 ms |
212 KB |
correct |
10 |
Correct |
1 ms |
212 KB |
correct |
11 |
Correct |
1 ms |
304 KB |
correct |
12 |
Correct |
0 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
1 ms |
340 KB |
correct |
15 |
Correct |
1 ms |
308 KB |
correct |
16 |
Correct |
1 ms |
340 KB |
correct |
17 |
Correct |
1 ms |
308 KB |
correct |
18 |
Correct |
1 ms |
340 KB |
correct |
19 |
Correct |
2 ms |
340 KB |
correct |
20 |
Correct |
1 ms |
340 KB |
correct |
21 |
Correct |
1 ms |
340 KB |
correct |
22 |
Correct |
1 ms |
340 KB |
correct |
23 |
Correct |
1 ms |
304 KB |
correct |
24 |
Correct |
1 ms |
300 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
340 KB |
correct |
27 |
Correct |
1 ms |
340 KB |
correct |
28 |
Correct |
1 ms |
212 KB |
correct |
29 |
Correct |
1 ms |
212 KB |
correct |
30 |
Correct |
2 ms |
340 KB |
correct |
31 |
Correct |
2 ms |
340 KB |
correct |
32 |
Correct |
1 ms |
340 KB |
correct |
33 |
Correct |
1 ms |
340 KB |
correct |
34 |
Correct |
25 ms |
1332 KB |
correct |
35 |
Correct |
27 ms |
1268 KB |
correct |
36 |
Correct |
28 ms |
1556 KB |
correct |
37 |
Correct |
12 ms |
496 KB |
correct |
38 |
Correct |
27 ms |
1372 KB |
correct |
39 |
Correct |
27 ms |
1424 KB |
correct |
40 |
Correct |
29 ms |
1492 KB |
correct |
41 |
Correct |
26 ms |
1364 KB |
correct |
42 |
Correct |
27 ms |
1364 KB |
correct |
43 |
Correct |
22 ms |
1320 KB |
correct |
44 |
Correct |
21 ms |
1236 KB |
correct |
45 |
Correct |
23 ms |
1496 KB |
correct |
46 |
Correct |
20 ms |
1236 KB |
correct |
47 |
Correct |
17 ms |
892 KB |
correct |
48 |
Correct |
7 ms |
308 KB |
correct |
49 |
Correct |
11 ms |
468 KB |
correct |
50 |
Correct |
17 ms |
876 KB |
correct |
51 |
Correct |
23 ms |
1436 KB |
correct |
52 |
Correct |
22 ms |
1364 KB |
correct |
53 |
Correct |
20 ms |
1240 KB |
correct |
54 |
Correct |
22 ms |
1236 KB |
correct |
55 |
Correct |
23 ms |
1492 KB |
correct |
56 |
Correct |
23 ms |
1492 KB |
correct |
57 |
Correct |
25 ms |
1468 KB |
correct |
58 |
Correct |
1 ms |
300 KB |
correct |
59 |
Correct |
1 ms |
212 KB |
correct |
60 |
Correct |
84 ms |
3432 KB |
correct |
61 |
Correct |
123 ms |
4812 KB |
correct |
62 |
Correct |
128 ms |
4888 KB |
correct |
63 |
Correct |
131 ms |
4884 KB |
correct |
64 |
Correct |
140 ms |
4980 KB |
correct |
65 |
Correct |
123 ms |
4864 KB |
correct |
66 |
Correct |
124 ms |
4792 KB |
correct |
67 |
Correct |
126 ms |
4884 KB |
correct |
68 |
Correct |
144 ms |
4812 KB |
correct |
69 |
Correct |
126 ms |
4876 KB |
correct |
70 |
Correct |
0 ms |
212 KB |
correct |
71 |
Correct |
134 ms |
4852 KB |
correct |
72 |
Correct |
136 ms |
4980 KB |
correct |
73 |
Correct |
1 ms |
212 KB |
correct |
74 |
Correct |
129 ms |
4752 KB |
correct |
75 |
Correct |
135 ms |
4828 KB |
correct |
76 |
Correct |
87 ms |
3680 KB |
correct |
77 |
Correct |
141 ms |
4864 KB |
correct |
78 |
Correct |
125 ms |
4896 KB |
correct |
79 |
Correct |
131 ms |
4896 KB |
correct |
80 |
Correct |
133 ms |
4800 KB |
correct |
81 |
Correct |
43 ms |
4180 KB |
correct |
82 |
Correct |
139 ms |
4852 KB |
correct |
83 |
Correct |
133 ms |
5132 KB |
correct |
84 |
Correct |
108 ms |
4236 KB |
correct |
85 |
Correct |
115 ms |
4832 KB |
correct |
86 |
Correct |
110 ms |
4384 KB |
correct |
87 |
Correct |
97 ms |
3796 KB |
correct |
88 |
Correct |
91 ms |
3148 KB |
correct |
89 |
Correct |
88 ms |
2900 KB |
correct |
90 |
Correct |
85 ms |
2644 KB |
correct |
91 |
Correct |
41 ms |
596 KB |
correct |
92 |
Correct |
29 ms |
468 KB |
correct |
93 |
Correct |
120 ms |
4780 KB |
correct |
94 |
Correct |
115 ms |
4408 KB |
correct |
95 |
Correct |
103 ms |
4412 KB |
correct |
96 |
Correct |
87 ms |
2756 KB |
correct |
97 |
Correct |
93 ms |
3156 KB |
correct |
98 |
Correct |
113 ms |
3796 KB |
correct |
99 |
Correct |
107 ms |
3140 KB |
correct |
100 |
Correct |
61 ms |
1140 KB |
correct |
101 |
Correct |
29 ms |
448 KB |
correct |
102 |
Correct |
116 ms |
5536 KB |
correct |
103 |
Correct |
125 ms |
5536 KB |
correct |
104 |
Correct |
123 ms |
5488 KB |
correct |
105 |
Correct |
114 ms |
5452 KB |
correct |