#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> p;
DSU() = default;
DSU(int n) : p(n) {
iota(p.begin(), p.end(), 0);
}
int leader(int x) {
while (x != p[x]) x = p[x] = p[p[x]];
return x;
}
bool unite(int x, int y) {
x = leader(x), y = leader(y);
if (x == y) {
return false;
}
p[y] = x;
return true;
}
bool same(int x, int y) {
return leader(x) == leader(y);
}
};
vector<int> find_roads(int n, std::vector<int> U, std::vector<int> V) {
const int m = U.size();
vector<int> type(m, -1);
vector idx(n, vector<int>(n, -1));
for (int i = 0; i < m; ++i) {
idx[U[i]][V[i]] = idx[V[i]][U[i]] = i;
}
DSU d1(n);
vector<int> st, par(n, -1), tin(n, -1);
vector<bool> in_st(m);
int T = 0;
auto dfs = [&](auto self, int v) -> void {
tin[v] = T++;
for (int to = 0; to < n; ++to) {
if (tin[to] == -1 && idx[v][to] != -1) {
par[to] = v;
in_st[idx[v][to]] = true;
st.push_back(idx[v][to]);
self(self, to);
}
}
};
dfs(dfs, 0);
auto path = [&](int x, int y) {
if (tin[x] > tin[y]) {
swap(x, y);
}
vector<int> ans;
while (y != x) {
ans.push_back(idx[y][par[y]]);
y = par[y];
}
return ans;
};
const int init = count_common_roads(st);
for (int i = 0; i < m; ++i) {
if (!in_st[i]) {
auto p = path(U[i], V[i]);
sort(p.begin(), p.end());
int done = -1, not_done = -1;
for (int x: p) {
if (type[x] != -1) {
done = x;
} else {
not_done = x;
}
}
if (done != -1) {
if (not_done == -1) {
continue;
}
set<int> sp(st.begin(), st.end());
sp.insert(i);
sp.erase(done);
int aim = count_common_roads(vector(sp.begin(), sp.end()));
sp.insert(done);
for (int x: p) {
if (type[x] == -1) {
sp.erase(x);
int now = count_common_roads(vector(sp.begin(), sp.end()));
sp.insert(x);
type[x] = type[done] ^ (now != aim);
}
}
} else {
set<int> sp(st.begin(), st.end());
sp.insert(i);
int mx = init, mn = init;
vector<pair<int, int>> save{{init, i}};
for (int x: p) {
sp.erase(x);
int now = count_common_roads(vector(sp.begin(), sp.end()));
sp.insert(x);
mx = max(mx, now), mn = min(mn, now);
save.emplace_back(now, x);
}
for (auto [now, x]: save) {
if (mn == mx) {
type[x] = 0;
} else {
type[x] = now != mx;
}
}
}
}
}
for (int i : st) {
if (type[i] == -1) {
type[i] = 1;
}
}
auto howMany = [&](vector<int> a) {
DSU d(n);
for (int i: a) {
assert(d.unite(U[i], V[i]));
}
int cnt = 0;
for (int p : st) {
if (d.unite(U[p], V[p])) {
a.push_back(p);
cnt += type[p];
}
}
assert(a.size() == n - 1);
return count_common_roads(a) - cnt;
};
for (int v = 0; v < n; ++v) {
vector<int> need;
for (int to = 0; to < n; ++to) {
if (idx[v][to] != -1 && type[idx[v][to]] == -1) {
need.push_back(idx[v][to]);
}
}
auto solve = [&](auto self, vector<int> a, int cnt) -> void {
if (cnt == 0) {
for (int i : a) {
type[i] = 0;
}
return;
}
if (a.size() == 1) {
type[a[0]] = 1;
return;
}
vector<int> L(a.begin(), a.begin() + a.size() / 2);
vector<int> R(a.begin() + a.size() / 2, a.end());
int have = howMany(L);
self(self, L, have);
self(self, R, cnt - have);
};
solve(solve, need, howMany(need));
}
st.clear();
for (int i = 0; i < m; ++i) {
assert(type[i] != -1);
if (type[i] == 1) {
st.push_back(i);
}
}
assert(st.size() == n - 1);
return st;
}
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from simurgh.cpp:2:
simurgh.cpp: In lambda function:
simurgh.cpp:152:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
152 | assert(a.size() == n - 1);
| ~~~~~~~~~^~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:192:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
192 | assert(st.size() == n - 1);
| ~~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
296 KB |
correct |
7 |
Correct |
0 ms |
212 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
1 ms |
300 KB |
correct |
10 |
Correct |
0 ms |
296 KB |
correct |
11 |
Correct |
0 ms |
212 KB |
correct |
12 |
Correct |
1 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
296 KB |
correct |
7 |
Correct |
0 ms |
212 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
1 ms |
300 KB |
correct |
10 |
Correct |
0 ms |
296 KB |
correct |
11 |
Correct |
0 ms |
212 KB |
correct |
12 |
Correct |
1 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
2 ms |
340 KB |
correct |
15 |
Correct |
2 ms |
340 KB |
correct |
16 |
Correct |
2 ms |
308 KB |
correct |
17 |
Correct |
2 ms |
340 KB |
correct |
18 |
Correct |
2 ms |
424 KB |
correct |
19 |
Correct |
2 ms |
340 KB |
correct |
20 |
Correct |
2 ms |
340 KB |
correct |
21 |
Correct |
2 ms |
340 KB |
correct |
22 |
Correct |
1 ms |
308 KB |
correct |
23 |
Correct |
2 ms |
212 KB |
correct |
24 |
Correct |
1 ms |
212 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
212 KB |
correct |
27 |
Correct |
1 ms |
212 KB |
correct |
28 |
Correct |
1 ms |
212 KB |
correct |
29 |
Correct |
1 ms |
212 KB |
correct |
30 |
Correct |
2 ms |
212 KB |
correct |
31 |
Correct |
2 ms |
340 KB |
correct |
32 |
Correct |
2 ms |
300 KB |
correct |
33 |
Correct |
2 ms |
340 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
296 KB |
correct |
7 |
Correct |
0 ms |
212 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
1 ms |
300 KB |
correct |
10 |
Correct |
0 ms |
296 KB |
correct |
11 |
Correct |
0 ms |
212 KB |
correct |
12 |
Correct |
1 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
2 ms |
340 KB |
correct |
15 |
Correct |
2 ms |
340 KB |
correct |
16 |
Correct |
2 ms |
308 KB |
correct |
17 |
Correct |
2 ms |
340 KB |
correct |
18 |
Correct |
2 ms |
424 KB |
correct |
19 |
Correct |
2 ms |
340 KB |
correct |
20 |
Correct |
2 ms |
340 KB |
correct |
21 |
Correct |
2 ms |
340 KB |
correct |
22 |
Correct |
1 ms |
308 KB |
correct |
23 |
Correct |
2 ms |
212 KB |
correct |
24 |
Correct |
1 ms |
212 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
212 KB |
correct |
27 |
Correct |
1 ms |
212 KB |
correct |
28 |
Correct |
1 ms |
212 KB |
correct |
29 |
Correct |
1 ms |
212 KB |
correct |
30 |
Correct |
2 ms |
212 KB |
correct |
31 |
Correct |
2 ms |
340 KB |
correct |
32 |
Correct |
2 ms |
300 KB |
correct |
33 |
Correct |
2 ms |
340 KB |
correct |
34 |
Correct |
104 ms |
1324 KB |
correct |
35 |
Correct |
100 ms |
1292 KB |
correct |
36 |
Correct |
74 ms |
1052 KB |
correct |
37 |
Correct |
12 ms |
600 KB |
correct |
38 |
Correct |
104 ms |
1380 KB |
correct |
39 |
Correct |
88 ms |
1324 KB |
correct |
40 |
Correct |
71 ms |
1072 KB |
correct |
41 |
Correct |
101 ms |
1312 KB |
correct |
42 |
Correct |
105 ms |
1328 KB |
correct |
43 |
Correct |
41 ms |
960 KB |
correct |
44 |
Correct |
32 ms |
852 KB |
correct |
45 |
Correct |
39 ms |
932 KB |
correct |
46 |
Correct |
28 ms |
852 KB |
correct |
47 |
Correct |
16 ms |
696 KB |
correct |
48 |
Correct |
6 ms |
468 KB |
correct |
49 |
Correct |
9 ms |
592 KB |
correct |
50 |
Correct |
15 ms |
700 KB |
correct |
51 |
Correct |
37 ms |
924 KB |
correct |
52 |
Correct |
38 ms |
852 KB |
correct |
53 |
Correct |
28 ms |
840 KB |
correct |
54 |
Correct |
42 ms |
956 KB |
correct |
55 |
Correct |
58 ms |
932 KB |
correct |
56 |
Correct |
57 ms |
944 KB |
correct |
57 |
Correct |
57 ms |
972 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
correct |
2 |
Correct |
1 ms |
212 KB |
correct |
3 |
Correct |
467 ms |
3280 KB |
correct |
4 |
Correct |
885 ms |
4896 KB |
correct |
5 |
Correct |
889 ms |
4760 KB |
correct |
6 |
Correct |
883 ms |
4760 KB |
correct |
7 |
Correct |
850 ms |
4804 KB |
correct |
8 |
Correct |
854 ms |
4768 KB |
correct |
9 |
Correct |
883 ms |
4768 KB |
correct |
10 |
Correct |
921 ms |
4872 KB |
correct |
11 |
Correct |
892 ms |
4808 KB |
correct |
12 |
Correct |
893 ms |
4772 KB |
correct |
13 |
Correct |
1 ms |
212 KB |
correct |
14 |
Correct |
890 ms |
4764 KB |
correct |
15 |
Correct |
891 ms |
4812 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
296 KB |
correct |
7 |
Correct |
0 ms |
212 KB |
correct |
8 |
Correct |
0 ms |
212 KB |
correct |
9 |
Correct |
1 ms |
300 KB |
correct |
10 |
Correct |
0 ms |
296 KB |
correct |
11 |
Correct |
0 ms |
212 KB |
correct |
12 |
Correct |
1 ms |
212 KB |
correct |
13 |
Correct |
0 ms |
212 KB |
correct |
14 |
Correct |
2 ms |
340 KB |
correct |
15 |
Correct |
2 ms |
340 KB |
correct |
16 |
Correct |
2 ms |
308 KB |
correct |
17 |
Correct |
2 ms |
340 KB |
correct |
18 |
Correct |
2 ms |
424 KB |
correct |
19 |
Correct |
2 ms |
340 KB |
correct |
20 |
Correct |
2 ms |
340 KB |
correct |
21 |
Correct |
2 ms |
340 KB |
correct |
22 |
Correct |
1 ms |
308 KB |
correct |
23 |
Correct |
2 ms |
212 KB |
correct |
24 |
Correct |
1 ms |
212 KB |
correct |
25 |
Correct |
1 ms |
212 KB |
correct |
26 |
Correct |
1 ms |
212 KB |
correct |
27 |
Correct |
1 ms |
212 KB |
correct |
28 |
Correct |
1 ms |
212 KB |
correct |
29 |
Correct |
1 ms |
212 KB |
correct |
30 |
Correct |
2 ms |
212 KB |
correct |
31 |
Correct |
2 ms |
340 KB |
correct |
32 |
Correct |
2 ms |
300 KB |
correct |
33 |
Correct |
2 ms |
340 KB |
correct |
34 |
Correct |
104 ms |
1324 KB |
correct |
35 |
Correct |
100 ms |
1292 KB |
correct |
36 |
Correct |
74 ms |
1052 KB |
correct |
37 |
Correct |
12 ms |
600 KB |
correct |
38 |
Correct |
104 ms |
1380 KB |
correct |
39 |
Correct |
88 ms |
1324 KB |
correct |
40 |
Correct |
71 ms |
1072 KB |
correct |
41 |
Correct |
101 ms |
1312 KB |
correct |
42 |
Correct |
105 ms |
1328 KB |
correct |
43 |
Correct |
41 ms |
960 KB |
correct |
44 |
Correct |
32 ms |
852 KB |
correct |
45 |
Correct |
39 ms |
932 KB |
correct |
46 |
Correct |
28 ms |
852 KB |
correct |
47 |
Correct |
16 ms |
696 KB |
correct |
48 |
Correct |
6 ms |
468 KB |
correct |
49 |
Correct |
9 ms |
592 KB |
correct |
50 |
Correct |
15 ms |
700 KB |
correct |
51 |
Correct |
37 ms |
924 KB |
correct |
52 |
Correct |
38 ms |
852 KB |
correct |
53 |
Correct |
28 ms |
840 KB |
correct |
54 |
Correct |
42 ms |
956 KB |
correct |
55 |
Correct |
58 ms |
932 KB |
correct |
56 |
Correct |
57 ms |
944 KB |
correct |
57 |
Correct |
57 ms |
972 KB |
correct |
58 |
Correct |
1 ms |
212 KB |
correct |
59 |
Correct |
1 ms |
212 KB |
correct |
60 |
Correct |
467 ms |
3280 KB |
correct |
61 |
Correct |
885 ms |
4896 KB |
correct |
62 |
Correct |
889 ms |
4760 KB |
correct |
63 |
Correct |
883 ms |
4760 KB |
correct |
64 |
Correct |
850 ms |
4804 KB |
correct |
65 |
Correct |
854 ms |
4768 KB |
correct |
66 |
Correct |
883 ms |
4768 KB |
correct |
67 |
Correct |
921 ms |
4872 KB |
correct |
68 |
Correct |
892 ms |
4808 KB |
correct |
69 |
Correct |
893 ms |
4772 KB |
correct |
70 |
Correct |
1 ms |
212 KB |
correct |
71 |
Correct |
890 ms |
4764 KB |
correct |
72 |
Correct |
891 ms |
4812 KB |
correct |
73 |
Correct |
1 ms |
212 KB |
correct |
74 |
Correct |
895 ms |
4836 KB |
correct |
75 |
Correct |
890 ms |
4644 KB |
correct |
76 |
Correct |
287 ms |
2252 KB |
correct |
77 |
Correct |
908 ms |
4876 KB |
correct |
78 |
Correct |
924 ms |
4888 KB |
correct |
79 |
Correct |
900 ms |
4856 KB |
correct |
80 |
Correct |
861 ms |
4764 KB |
correct |
81 |
Correct |
715 ms |
4152 KB |
correct |
82 |
Correct |
851 ms |
4644 KB |
correct |
83 |
Correct |
479 ms |
3148 KB |
correct |
84 |
Correct |
405 ms |
3368 KB |
correct |
85 |
Correct |
301 ms |
3140 KB |
correct |
86 |
Correct |
176 ms |
2528 KB |
correct |
87 |
Correct |
118 ms |
2216 KB |
correct |
88 |
Correct |
100 ms |
2008 KB |
correct |
89 |
Correct |
95 ms |
1952 KB |
correct |
90 |
Correct |
86 ms |
1844 KB |
correct |
91 |
Correct |
38 ms |
1384 KB |
correct |
92 |
Correct |
25 ms |
1236 KB |
correct |
93 |
Correct |
361 ms |
3148 KB |
correct |
94 |
Correct |
183 ms |
2592 KB |
correct |
95 |
Correct |
161 ms |
2484 KB |
correct |
96 |
Correct |
86 ms |
1840 KB |
correct |
97 |
Correct |
109 ms |
2012 KB |
correct |
98 |
Correct |
128 ms |
2176 KB |
correct |
99 |
Correct |
100 ms |
2040 KB |
correct |
100 |
Correct |
51 ms |
1500 KB |
correct |
101 |
Correct |
26 ms |
1364 KB |
correct |
102 |
Correct |
482 ms |
3080 KB |
correct |
103 |
Correct |
486 ms |
3100 KB |
correct |
104 |
Correct |
468 ms |
3108 KB |
correct |
105 |
Correct |
474 ms |
3148 KB |
correct |