#include <vector>
#include "incursion.h"
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
std::vector<int> mark(std::vector<std::pair<int, int>> f, int safe) {
int n = f.size() + 1;
safe -= 1;
vector<int> sz(n), maxsz(n);
vector<vector<int>> adj(n);
vector<int> par(n, -1), to_safe(n, -2);
for (auto [u, v] : f) {
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
function<void(int, int)> dfs_sz = [&](int node, int par) {
sz[node] = 1;
maxsz[node] = 0;
for (auto v : adj[node]) {
if (v == par) continue;
dfs_sz(v, node);
maxsz[node] = max(maxsz[node], sz[v]);
sz[node] += sz[v];
}
maxsz[node] = max(maxsz[node], n - sz[node]);
};
dfs_sz(0, -1);
// check centroid count
int cent_req = *min_element(maxsz.begin(), maxsz.end());
vector<int> centroids;
for (int i = 0; i < n; ++i) {
if (maxsz[i] == cent_req) {
centroids.push_back(i);
}
}
assert(0 < centroids.size() && centroids.size() <= 2);
function<void(int, vector<int>&)> dfs_par = [&](int node, vector<int>& par) {
for (auto v : adj[node]) {
if (v == par[node]) continue;
par[v] = node;
dfs_par(v, par);
}
};
dfs_par(centroids[0], par);
dfs_par(safe, to_safe);
if (centroids.size() == 2) {
par[centroids[0]] = centroids[1];
}
vector<int> ret;
// true -> go to parent
// false -> go to other child
for (int i = 0; i < n; ++i) {
if (to_safe[i] == par[i]) {
ret.push_back(true);
} else {
ret.push_back(false);
}
}
return ret;
}
void locate(std::vector<std::pair<int, int>> F, int node, int t) {
node -= 1;
int n = F.size() + 1;
vector<vector<int>> adj(n);
vector<int> sz(n), maxsz(n);
vector<int> par(n, -1);
vector<bool> visited(n, false);
visited[node] = true;
for (auto [u, v] : F) {
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}
function<void(int, int)> dfs_sz = [&](int node, int par) {
sz[node] = 1;
maxsz[node] = 0;
for (auto v : adj[node]) {
if (v == par) continue;
dfs_sz(v, node);
maxsz[node] = max(maxsz[node], sz[v]);
sz[node] += sz[v];
}
maxsz[node] = max(maxsz[node], n - sz[node]);
};
dfs_sz(0, -1);
// check centroid count
int cent_req = *min_element(maxsz.begin(), maxsz.end());
vector<int> centroids;
for (int i = 0; i < n; ++i) {
if (maxsz[i] == cent_req) {
centroids.push_back(i);
}
}
assert(0 < centroids.size() && centroids.size() <= 2);
function<void(int, vector<int>&)> dfs_par = [&](int node, vector<int>& par) {
for (auto v : adj[node]) {
if (v == par[node]) continue;
par[v] = node;
dfs_par(v, par);
}
};
dfs_par(centroids[0], par);
if (centroids.size() == 2) {
par[centroids[0]] = centroids[1];
}
auto go = [&](int node) {
visited[node] = true;
return visit(node + 1);
};
while (true) {
if (t) {
if (visited[par[node]]) {
go(par[node]);
break;
}
t = go(par[node]);
node = par[node];
continue;
}
// node the singular centroid
// try all ways
bool moved = false;
for (auto v : adj[node]) {
if (v == par[node]) continue;
if (visited[v]) continue;
int ct = go(v);
if (ct) {
go(node);
} else {
t = ct;
node = v;
moved = true;
break;
}
}
if (!moved) break;
}
}
#ifndef EVAL
#include "sample_grader.cpp"
#endif
Compilation message
interface.cpp: In function 'int main()':
interface.cpp:44:55: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | if(fread(T.data(), sizeof(int), 2 * N - 2, stdin) != 2 * N - 2) exit(0);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
interface.cpp:50:33: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
50 | int l = (numbers.size() == N ? N : 0);
| ~~~~~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
764 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
189 ms |
15328 KB |
Correct |
2 |
Correct |
185 ms |
16236 KB |
Correct |
3 |
Correct |
107 ms |
16432 KB |
Correct |
4 |
Correct |
87 ms |
16416 KB |
Correct |
5 |
Correct |
175 ms |
16412 KB |
Correct |
6 |
Correct |
80 ms |
15968 KB |
Correct |
7 |
Correct |
92 ms |
14188 KB |
Correct |
8 |
Correct |
187 ms |
15464 KB |
Correct |
9 |
Correct |
197 ms |
16948 KB |
Correct |
10 |
Correct |
128 ms |
15112 KB |
Correct |
11 |
Correct |
100 ms |
16200 KB |
Correct |
12 |
Correct |
216 ms |
15288 KB |
Correct |
13 |
Correct |
87 ms |
13976 KB |
Correct |
14 |
Correct |
86 ms |
14420 KB |
Correct |
15 |
Correct |
232 ms |
16204 KB |
Correct |
16 |
Correct |
188 ms |
16792 KB |
Correct |
17 |
Correct |
115 ms |
13916 KB |
Correct |
18 |
Correct |
87 ms |
14176 KB |
Correct |
19 |
Correct |
139 ms |
13952 KB |
Correct |
20 |
Correct |
86 ms |
15788 KB |
Correct |
21 |
Correct |
82 ms |
14492 KB |
Correct |
22 |
Correct |
178 ms |
16116 KB |
Correct |
23 |
Correct |
195 ms |
16660 KB |
Correct |
24 |
Correct |
91 ms |
14500 KB |
Correct |
25 |
Correct |
79 ms |
15196 KB |
Correct |
26 |
Correct |
101 ms |
14100 KB |
Correct |
27 |
Correct |
70 ms |
14432 KB |
Correct |
28 |
Correct |
82 ms |
14960 KB |
Correct |
29 |
Correct |
199 ms |
16868 KB |
Correct |
30 |
Correct |
186 ms |
15300 KB |
Correct |
31 |
Correct |
84 ms |
16288 KB |
Correct |
32 |
Correct |
202 ms |
14912 KB |
Correct |
33 |
Correct |
207 ms |
15336 KB |
Correct |
34 |
Correct |
81 ms |
13856 KB |
Correct |
35 |
Correct |
66 ms |
13324 KB |
Correct |
36 |
Correct |
211 ms |
16072 KB |
Correct |
37 |
Correct |
203 ms |
16888 KB |
Correct |
38 |
Correct |
229 ms |
15744 KB |
Correct |
39 |
Correct |
130 ms |
16364 KB |
Correct |
40 |
Correct |
153 ms |
15584 KB |
Correct |
41 |
Correct |
75 ms |
15260 KB |
Correct |
42 |
Correct |
75 ms |
15244 KB |
Correct |
43 |
Correct |
172 ms |
16332 KB |
Correct |
44 |
Correct |
193 ms |
15452 KB |
Correct |
45 |
Correct |
80 ms |
14496 KB |
Correct |
46 |
Correct |
64 ms |
15456 KB |
Correct |
47 |
Correct |
89 ms |
15628 KB |
Correct |
48 |
Correct |
76 ms |
13464 KB |
Correct |
49 |
Correct |
68 ms |
15832 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
8792 KB |
Correct |
2 |
Incorrect |
69 ms |
8736 KB |
Not correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
764 KB |
Correct |
2 |
Correct |
189 ms |
15328 KB |
Correct |
3 |
Correct |
185 ms |
16236 KB |
Correct |
4 |
Correct |
107 ms |
16432 KB |
Correct |
5 |
Correct |
87 ms |
16416 KB |
Correct |
6 |
Correct |
175 ms |
16412 KB |
Correct |
7 |
Correct |
80 ms |
15968 KB |
Correct |
8 |
Correct |
92 ms |
14188 KB |
Correct |
9 |
Correct |
187 ms |
15464 KB |
Correct |
10 |
Correct |
197 ms |
16948 KB |
Correct |
11 |
Correct |
128 ms |
15112 KB |
Correct |
12 |
Correct |
100 ms |
16200 KB |
Correct |
13 |
Correct |
216 ms |
15288 KB |
Correct |
14 |
Correct |
87 ms |
13976 KB |
Correct |
15 |
Correct |
86 ms |
14420 KB |
Correct |
16 |
Correct |
232 ms |
16204 KB |
Correct |
17 |
Correct |
188 ms |
16792 KB |
Correct |
18 |
Correct |
115 ms |
13916 KB |
Correct |
19 |
Correct |
87 ms |
14176 KB |
Correct |
20 |
Correct |
139 ms |
13952 KB |
Correct |
21 |
Correct |
86 ms |
15788 KB |
Correct |
22 |
Correct |
82 ms |
14492 KB |
Correct |
23 |
Correct |
178 ms |
16116 KB |
Correct |
24 |
Correct |
195 ms |
16660 KB |
Correct |
25 |
Correct |
91 ms |
14500 KB |
Correct |
26 |
Correct |
79 ms |
15196 KB |
Correct |
27 |
Correct |
101 ms |
14100 KB |
Correct |
28 |
Correct |
70 ms |
14432 KB |
Correct |
29 |
Correct |
82 ms |
14960 KB |
Correct |
30 |
Correct |
199 ms |
16868 KB |
Correct |
31 |
Correct |
186 ms |
15300 KB |
Correct |
32 |
Correct |
84 ms |
16288 KB |
Correct |
33 |
Correct |
202 ms |
14912 KB |
Correct |
34 |
Correct |
207 ms |
15336 KB |
Correct |
35 |
Correct |
81 ms |
13856 KB |
Correct |
36 |
Correct |
66 ms |
13324 KB |
Correct |
37 |
Correct |
211 ms |
16072 KB |
Correct |
38 |
Correct |
203 ms |
16888 KB |
Correct |
39 |
Correct |
229 ms |
15744 KB |
Correct |
40 |
Correct |
130 ms |
16364 KB |
Correct |
41 |
Correct |
153 ms |
15584 KB |
Correct |
42 |
Correct |
75 ms |
15260 KB |
Correct |
43 |
Correct |
75 ms |
15244 KB |
Correct |
44 |
Correct |
172 ms |
16332 KB |
Correct |
45 |
Correct |
193 ms |
15452 KB |
Correct |
46 |
Correct |
80 ms |
14496 KB |
Correct |
47 |
Correct |
64 ms |
15456 KB |
Correct |
48 |
Correct |
89 ms |
15628 KB |
Correct |
49 |
Correct |
76 ms |
13464 KB |
Correct |
50 |
Correct |
68 ms |
15832 KB |
Correct |
51 |
Correct |
71 ms |
8792 KB |
Correct |
52 |
Incorrect |
69 ms |
8736 KB |
Not correct |
53 |
Halted |
0 ms |
0 KB |
- |