#include<bits/stdc++.h>
#include "simurgh.h"
using namespace std;
const int N = 510 + 10;
struct DSU {
vector<int> lab;
void init (int sz) {
lab.resize(sz + 2);
for(int i = 0; i <= sz; i++) lab[i] = -1;
}
int root(int v) {
return lab[v] < 0 ? v : lab[v] = root(lab[v]);
}
bool join(int u, int v) {
u = root(u);
v = root(v);
if (u == v) return false;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
};
DSU edge;
DSU tree[N];
vector<int> adj[N], _adj[N], st, arr[N];
int h[N], par[N], __u[N * N], __v[N * N];
bool in[N * N], taken[N], vis[N], royal[N * N];
int idx[N], know[N];
void dfs(int u, int p) {
h[u] = h[p] + 1;
par[u] = p;
for(int &v : adj[u]) if (v != p) dfs(v, u);
}
void tell (int u) {
vis[u] = 1;
for(int &v : _adj[u]) if (know[v] == -1) {
know[v] = know[u];
tell(v);
}
}
int calc (vector<int> a) {
DSU tmp;
tmp.init(st.size() + 2);
vector<int> ask = a;
for(int &j : a) assert(tmp.join(__u[j], __v[j]));
int sum = 0;
for(int &j : st) if (tmp.join(__u[j], __v[j])) {
ask.push_back(j);
sum += royal[j];
}
assert(ask.size() == st.size());
return count_common_roads(ask) - sum;
}
void solve(vector<int> cur, int tmp) {
if (!tmp) return;
// for(auto &j : cur) cout << j << " "; cout << tmp << "@@" << endl;
if (cur.size() == 1) {
royal[cur.back()] = tmp;
return;
}
vector<int> left, right;
for(int i = 0; i < cur.size(); i++) {
if (i < (int) cur.size() / 2) left.push_back(cur[i]);
else right.push_back(cur[i]);
}
// if (left.size() >= cur.size()) {
// cerr << cur.size() << endl;
// }
assert(left.size() < cur.size());
int cost_left = calc(left);
int cost_right = tmp - cost_left;
solve(left, cost_left);
solve(right, cost_right);
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
int m = u.size();
edge.init(n);
for(int i = 0; i < m; i++) __u[i] = u[i], __v[i] = v[i];
tree[0].init(n);
for(int i = 0; i < m; i++) {
if (tree[0].join(u[i], v[i])) {
st.push_back(i);
adj[u[i]].push_back(v[i]);
adj[v[i]].push_back(u[i]);
in[i] = 1;
}
}
// for(int &j : st) cout << j << " "; cout << endl;
int init = count_common_roads(st);
dfs(0, 0);
for(int i = 0; i < n; i++) know[i] = -1;
// exit(0);
for(int i = 0; i < m; i++) if (in[i]) {
if (h[u[i]] < h[v[i]]) {
swap(u[i], v[i]);
swap(__u[i], __v[i]);
}
idx[u[i]] = i;
}
// exit(0);
for(int i = 0; i < m; i++) if (!in[i]) {
int x = u[i], y = v[i];
if (h[x] < h[y]) swap(x, y);
vector<int> part;
auto add = [&] (int a) {
if (a == 0) return;
int _a = edge.root(a);
if (!taken[_a]) {
taken[_a] = 1;
part.push_back(a);
}
};
while (h[x] > h[y]) {
add(x);
x = par[x];
}
while (x != y) {
add(x);
add(y);
x = par[x];
y = par[y];
}
for(int &j : part) taken[edge.root(j)] = 0;
if (part.size() <= 1) continue;
int know_i = -1;
// for(int &j : part) cout << j << " "; cout << endl;
// cout << i << "@\n";
for(int &j : part) {
vector<int> ask;
for(int &k : st) if (k != idx[j]) ask.push_back(k);
ask.push_back(i);
assert(ask.size() == st.size());
int tmp = count_common_roads(ask);
if (tmp < init) {
know[j] = 1;
know_i = 0;
}
else if (tmp > init) {
know_i = 1;
know[j] = 0;
}
else if (know[j] != -1) {
know_i = know[j];
}
}
int head = part[0];
for(int &j : part) edge.join(j, head);
if (know_i == -1) {
for(int &j : part) if (j != head) {
_adj[j].push_back(head);
_adj[head].push_back(j);
}
}
else {
for(int &j : part) {
if (know[j] == -1) know[j] = know_i;
if (!vis[j]) tell(j);
}
}
}
if (init == 0) for(int i = 0; i < n; i++) know[i] = 0;
if (init == n - 1) for(int i = 0; i < n; i++) know[i] = 1;
for(int i = 1; i < n; i++) {
if (know[i] == -1) know[i] = 1;
royal[idx[i]] = know[i];
}
//
// for(int &j : st) {
// cout << royal[j] << " " << u[j] << " " << v[j] << " " << j << endl;
// } exit(0);
for(int j = 1; j <= n; j++) tree[j].init(n);
for(int i = 0; i < m; i++) if (!in[i]) {
bool stop = 0;
for(int j = 1; j <= n; j++) if (tree[j].join(u[i], v[i])) {
arr[j].push_back(i);
stop = 1;
break;
}
assert(stop);
}
for(int j = 1; j <= n; j++) {
if (!arr[j].empty()) solve(arr[j], calc(arr[j]));
}
for(int i = 0; i < m; i++) assert(__u[i] == u[i] && __v[i] == v[i]);
#ifdef ngu
for(int i = 0; i < m; i++) if (royal[i]) cout << i << " "; cout << endl;
#endif // ngu
vector<int> ret;
for(int i = 0; i < m; i++) if (royal[i]) ret.push_back(i);
return ret;
}
Compilation message
simurgh.cpp: In function 'void solve(std::vector<int>, int)':
simurgh.cpp:79:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i = 0; i < cur.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
correct |
2 |
Correct |
1 ms |
2396 KB |
correct |
3 |
Correct |
1 ms |
2396 KB |
correct |
4 |
Correct |
1 ms |
2396 KB |
correct |
5 |
Correct |
1 ms |
2396 KB |
correct |
6 |
Correct |
1 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2392 KB |
correct |
9 |
Correct |
1 ms |
2396 KB |
correct |
10 |
Correct |
1 ms |
2396 KB |
correct |
11 |
Correct |
1 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
correct |
2 |
Correct |
1 ms |
2396 KB |
correct |
3 |
Correct |
1 ms |
2396 KB |
correct |
4 |
Correct |
1 ms |
2396 KB |
correct |
5 |
Correct |
1 ms |
2396 KB |
correct |
6 |
Correct |
1 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2392 KB |
correct |
9 |
Correct |
1 ms |
2396 KB |
correct |
10 |
Correct |
1 ms |
2396 KB |
correct |
11 |
Correct |
1 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
14 |
Correct |
2 ms |
2648 KB |
correct |
15 |
Correct |
2 ms |
2392 KB |
correct |
16 |
Correct |
1 ms |
2396 KB |
correct |
17 |
Correct |
1 ms |
2392 KB |
correct |
18 |
Correct |
1 ms |
2392 KB |
correct |
19 |
Correct |
2 ms |
2396 KB |
correct |
20 |
Correct |
1 ms |
2396 KB |
correct |
21 |
Correct |
1 ms |
2396 KB |
correct |
22 |
Correct |
1 ms |
2396 KB |
correct |
23 |
Correct |
1 ms |
2396 KB |
correct |
24 |
Correct |
1 ms |
2396 KB |
correct |
25 |
Correct |
1 ms |
2396 KB |
correct |
26 |
Correct |
1 ms |
2396 KB |
correct |
27 |
Correct |
1 ms |
2396 KB |
correct |
28 |
Correct |
1 ms |
2392 KB |
correct |
29 |
Correct |
1 ms |
2396 KB |
correct |
30 |
Correct |
1 ms |
2396 KB |
correct |
31 |
Correct |
1 ms |
2396 KB |
correct |
32 |
Correct |
1 ms |
2592 KB |
correct |
33 |
Correct |
1 ms |
2540 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
correct |
2 |
Correct |
1 ms |
2396 KB |
correct |
3 |
Correct |
1 ms |
2396 KB |
correct |
4 |
Correct |
1 ms |
2396 KB |
correct |
5 |
Correct |
1 ms |
2396 KB |
correct |
6 |
Correct |
1 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2392 KB |
correct |
9 |
Correct |
1 ms |
2396 KB |
correct |
10 |
Correct |
1 ms |
2396 KB |
correct |
11 |
Correct |
1 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
14 |
Correct |
2 ms |
2648 KB |
correct |
15 |
Correct |
2 ms |
2392 KB |
correct |
16 |
Correct |
1 ms |
2396 KB |
correct |
17 |
Correct |
1 ms |
2392 KB |
correct |
18 |
Correct |
1 ms |
2392 KB |
correct |
19 |
Correct |
2 ms |
2396 KB |
correct |
20 |
Correct |
1 ms |
2396 KB |
correct |
21 |
Correct |
1 ms |
2396 KB |
correct |
22 |
Correct |
1 ms |
2396 KB |
correct |
23 |
Correct |
1 ms |
2396 KB |
correct |
24 |
Correct |
1 ms |
2396 KB |
correct |
25 |
Correct |
1 ms |
2396 KB |
correct |
26 |
Correct |
1 ms |
2396 KB |
correct |
27 |
Correct |
1 ms |
2396 KB |
correct |
28 |
Correct |
1 ms |
2392 KB |
correct |
29 |
Correct |
1 ms |
2396 KB |
correct |
30 |
Correct |
1 ms |
2396 KB |
correct |
31 |
Correct |
1 ms |
2396 KB |
correct |
32 |
Correct |
1 ms |
2592 KB |
correct |
33 |
Correct |
1 ms |
2540 KB |
correct |
34 |
Correct |
32 ms |
3544 KB |
correct |
35 |
Correct |
37 ms |
3660 KB |
correct |
36 |
Correct |
23 ms |
3160 KB |
correct |
37 |
Correct |
6 ms |
2652 KB |
correct |
38 |
Correct |
32 ms |
3476 KB |
correct |
39 |
Correct |
28 ms |
3460 KB |
correct |
40 |
Correct |
23 ms |
3164 KB |
correct |
41 |
Correct |
32 ms |
3416 KB |
correct |
42 |
Correct |
31 ms |
3664 KB |
correct |
43 |
Correct |
12 ms |
3420 KB |
correct |
44 |
Correct |
7 ms |
3068 KB |
correct |
45 |
Correct |
8 ms |
3176 KB |
correct |
46 |
Correct |
6 ms |
2904 KB |
correct |
47 |
Correct |
3 ms |
2908 KB |
correct |
48 |
Correct |
1 ms |
2652 KB |
correct |
49 |
Correct |
2 ms |
2652 KB |
correct |
50 |
Correct |
4 ms |
2908 KB |
correct |
51 |
Correct |
8 ms |
3068 KB |
correct |
52 |
Correct |
7 ms |
2904 KB |
correct |
53 |
Correct |
6 ms |
2904 KB |
correct |
54 |
Correct |
9 ms |
3164 KB |
correct |
55 |
Correct |
8 ms |
3164 KB |
correct |
56 |
Correct |
8 ms |
3164 KB |
correct |
57 |
Correct |
8 ms |
3164 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
correct |
2 |
Correct |
1 ms |
2396 KB |
correct |
3 |
Correct |
111 ms |
4944 KB |
correct |
4 |
Correct |
193 ms |
6996 KB |
correct |
5 |
Correct |
191 ms |
7172 KB |
correct |
6 |
Correct |
191 ms |
6996 KB |
correct |
7 |
Correct |
194 ms |
6992 KB |
correct |
8 |
Correct |
194 ms |
7024 KB |
correct |
9 |
Correct |
191 ms |
7004 KB |
correct |
10 |
Correct |
199 ms |
7012 KB |
correct |
11 |
Correct |
189 ms |
6992 KB |
correct |
12 |
Correct |
190 ms |
6992 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
14 |
Correct |
201 ms |
7224 KB |
correct |
15 |
Correct |
188 ms |
7060 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
correct |
2 |
Correct |
1 ms |
2396 KB |
correct |
3 |
Correct |
1 ms |
2396 KB |
correct |
4 |
Correct |
1 ms |
2396 KB |
correct |
5 |
Correct |
1 ms |
2396 KB |
correct |
6 |
Correct |
1 ms |
2396 KB |
correct |
7 |
Correct |
1 ms |
2396 KB |
correct |
8 |
Correct |
1 ms |
2392 KB |
correct |
9 |
Correct |
1 ms |
2396 KB |
correct |
10 |
Correct |
1 ms |
2396 KB |
correct |
11 |
Correct |
1 ms |
2396 KB |
correct |
12 |
Correct |
1 ms |
2396 KB |
correct |
13 |
Correct |
1 ms |
2396 KB |
correct |
14 |
Correct |
2 ms |
2648 KB |
correct |
15 |
Correct |
2 ms |
2392 KB |
correct |
16 |
Correct |
1 ms |
2396 KB |
correct |
17 |
Correct |
1 ms |
2392 KB |
correct |
18 |
Correct |
1 ms |
2392 KB |
correct |
19 |
Correct |
2 ms |
2396 KB |
correct |
20 |
Correct |
1 ms |
2396 KB |
correct |
21 |
Correct |
1 ms |
2396 KB |
correct |
22 |
Correct |
1 ms |
2396 KB |
correct |
23 |
Correct |
1 ms |
2396 KB |
correct |
24 |
Correct |
1 ms |
2396 KB |
correct |
25 |
Correct |
1 ms |
2396 KB |
correct |
26 |
Correct |
1 ms |
2396 KB |
correct |
27 |
Correct |
1 ms |
2396 KB |
correct |
28 |
Correct |
1 ms |
2392 KB |
correct |
29 |
Correct |
1 ms |
2396 KB |
correct |
30 |
Correct |
1 ms |
2396 KB |
correct |
31 |
Correct |
1 ms |
2396 KB |
correct |
32 |
Correct |
1 ms |
2592 KB |
correct |
33 |
Correct |
1 ms |
2540 KB |
correct |
34 |
Correct |
32 ms |
3544 KB |
correct |
35 |
Correct |
37 ms |
3660 KB |
correct |
36 |
Correct |
23 ms |
3160 KB |
correct |
37 |
Correct |
6 ms |
2652 KB |
correct |
38 |
Correct |
32 ms |
3476 KB |
correct |
39 |
Correct |
28 ms |
3460 KB |
correct |
40 |
Correct |
23 ms |
3164 KB |
correct |
41 |
Correct |
32 ms |
3416 KB |
correct |
42 |
Correct |
31 ms |
3664 KB |
correct |
43 |
Correct |
12 ms |
3420 KB |
correct |
44 |
Correct |
7 ms |
3068 KB |
correct |
45 |
Correct |
8 ms |
3176 KB |
correct |
46 |
Correct |
6 ms |
2904 KB |
correct |
47 |
Correct |
3 ms |
2908 KB |
correct |
48 |
Correct |
1 ms |
2652 KB |
correct |
49 |
Correct |
2 ms |
2652 KB |
correct |
50 |
Correct |
4 ms |
2908 KB |
correct |
51 |
Correct |
8 ms |
3068 KB |
correct |
52 |
Correct |
7 ms |
2904 KB |
correct |
53 |
Correct |
6 ms |
2904 KB |
correct |
54 |
Correct |
9 ms |
3164 KB |
correct |
55 |
Correct |
8 ms |
3164 KB |
correct |
56 |
Correct |
8 ms |
3164 KB |
correct |
57 |
Correct |
8 ms |
3164 KB |
correct |
58 |
Correct |
0 ms |
2396 KB |
correct |
59 |
Correct |
1 ms |
2396 KB |
correct |
60 |
Correct |
111 ms |
4944 KB |
correct |
61 |
Correct |
193 ms |
6996 KB |
correct |
62 |
Correct |
191 ms |
7172 KB |
correct |
63 |
Correct |
191 ms |
6996 KB |
correct |
64 |
Correct |
194 ms |
6992 KB |
correct |
65 |
Correct |
194 ms |
7024 KB |
correct |
66 |
Correct |
191 ms |
7004 KB |
correct |
67 |
Correct |
199 ms |
7012 KB |
correct |
68 |
Correct |
189 ms |
6992 KB |
correct |
69 |
Correct |
190 ms |
6992 KB |
correct |
70 |
Correct |
1 ms |
2396 KB |
correct |
71 |
Correct |
201 ms |
7224 KB |
correct |
72 |
Correct |
188 ms |
7060 KB |
correct |
73 |
Correct |
1 ms |
2396 KB |
correct |
74 |
Correct |
192 ms |
6980 KB |
correct |
75 |
Correct |
188 ms |
7288 KB |
correct |
76 |
Correct |
62 ms |
4432 KB |
correct |
77 |
Correct |
189 ms |
7032 KB |
correct |
78 |
Correct |
191 ms |
6992 KB |
correct |
79 |
Correct |
188 ms |
6944 KB |
correct |
80 |
Correct |
180 ms |
6992 KB |
correct |
81 |
Correct |
154 ms |
6264 KB |
correct |
82 |
Correct |
182 ms |
6992 KB |
correct |
83 |
Correct |
97 ms |
5440 KB |
correct |
84 |
Correct |
58 ms |
5412 KB |
correct |
85 |
Correct |
48 ms |
5212 KB |
correct |
86 |
Correct |
31 ms |
4696 KB |
correct |
87 |
Correct |
19 ms |
4580 KB |
correct |
88 |
Correct |
16 ms |
4184 KB |
correct |
89 |
Correct |
14 ms |
4188 KB |
correct |
90 |
Correct |
13 ms |
4048 KB |
correct |
91 |
Correct |
5 ms |
3600 KB |
correct |
92 |
Correct |
4 ms |
3420 KB |
correct |
93 |
Correct |
56 ms |
5360 KB |
correct |
94 |
Correct |
29 ms |
4696 KB |
correct |
95 |
Correct |
27 ms |
4920 KB |
correct |
96 |
Correct |
13 ms |
3932 KB |
correct |
97 |
Correct |
15 ms |
4184 KB |
correct |
98 |
Correct |
20 ms |
4188 KB |
correct |
99 |
Correct |
16 ms |
4188 KB |
correct |
100 |
Correct |
7 ms |
3676 KB |
correct |
101 |
Correct |
5 ms |
3420 KB |
correct |
102 |
Correct |
46 ms |
5252 KB |
correct |
103 |
Correct |
46 ms |
5204 KB |
correct |
104 |
Correct |
48 ms |
5204 KB |
correct |
105 |
Correct |
45 ms |
5308 KB |
correct |