#include <bits/stdc++.h>
#include "grader.h"
using namespace std;
typedef int64_t ll;
typedef string str;
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;
while (n > 1) {
subtree.clear();
int centroid = get_centroid(root, par[root]);
get_subtree(centroid, par[centroid]);
if (query(subtree)) {
root = centroid;
n = sz[root];
} else {
used[centroid] = true;
n -= sz[centroid];
int cur = centroid;
while (par[cur] != -1) {
sz[par[cur]] -= sz[centroid];
cur = par[cur];
}
}
}
return root;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Number of queries: 4 |
2 |
Runtime error |
2 ms |
464 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
336 KB |
Number of queries: 8 |
2 |
Runtime error |
3 ms |
460 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
336 KB |
Number of queries: 9 |
2 |
Runtime error |
2 ms |
464 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |