#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
typedef string str;
int query(vector <int> islands);
int findEgg(int n, vector <pair <int, int>> edges) {
vector <int> g[n + 1];
int par[n + 1], sz[n + 1];
vector <bool> used(n + 1, false);
for (auto [a, b] : edges) {
g[a].push_back(b);
g[b].push_back(a);
}
function <void(int, int)> prepare = [&](int v, int p) {
par[v] = p;
sz[v] = 1;
for (int to : g[v]) {
if (to == p) {
continue;
}
prepare(to, v);
sz[v] += sz[to];
}
};
prepare(1, -1);
function <int(int, int)> get_centroid = [&](int v, int p) {
for (int to : g[v]) {
if (to == p || used[to]) {
continue;
}
if (sz[to] * 2 > n) {
return get_centroid(to, v);
}
}
return v;
};
vector <int> subtree;
function <void(int, int)> get_subtree = [&](int v, int p) {
subtree.push_back(v);
for (int to : g[v]) {
if (to == p || used[to]) {
continue;
}
get_subtree(to, v);
}
};
int root = 1;
do {
subtree.clear();
int centroid = get_centroid(root, par[root]);
get_subtree(centroid, par[centroid]);
if (query(subtree)) {
root = centroid;
} else {
used[centroid] = true;
int cur = centroid;
while (par[cur] != -1) {
sz[par[cur]] -= sz[centroid];
cur = par[cur];
}
}
} while ((int)subtree.size() > 1);
return subtree.back();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
336 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
464 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
512 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |