This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "grader.h"
//#include "grader.cpp"
using namespace std;
#define EMT ios::sync_with_stdio(0); cin.tie(0);
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using ld = long double;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
const double EPS = 1e-9;
vector<vector<int>> edge;
vector<int> sz;
vector<int> que;
vector<bool> ok;
vector<bool> is;
int _n;
void init(int n) {
_n = n;
edge.clear();
sz.clear();
is.clear();
ok.clear();
edge.resize(n + 1);
sz.resize(n + 1);
is.resize(n + 1);
ok.resize(n + 1);
}
int dfs(int u, int p) {
sz[u] = 1;
for(int v : edge[u])
if(v != p && ok[v])
sz[u] += dfs(v, u);
return sz[u];
}
int centroid(int u, int p, int n) {
for(int v : edge[u])
if(v != p && ok[v] && sz[v] > n / 2)
return centroid(v, u, n);
return u;
}
void go(int u, int p) {
que.push_back(u);
is[u] = 1;
for(int v : edge[u])
if(v != p && ok[v])
go(v, u);
}
int solve(int u) {
for(int a : que) {
is[a] = 0;
}
que.clear();
int n = dfs(u, u);
if(n == 1)
return u;
u = centroid(u, u, n);
dfs(u, u);
assert(sz[u] == n);
que.push_back(u);
is[u] = 1;
int cur = 0;
for(int v : edge[u]) {
if(ok[v] && cur + sz[v] <= n / 2)
go(v, u), cur += sz[v];
}
if(n == 2) {
int x = que.back();
que.pop_back();
if(query(que))
return que[0];
else
return x;
}
//cerr << cur << '\n';
assert(!que.empty());
int w = query(que);
if(w) {
for(int i = 1; i <= _n; i++)
if(!is[i]) {
ok[i] = 0;
//cerr << "deletea " << i << '\n';
}
//cerr << "owo " << que[0] << '\n';
return solve(u);
}
else {
for(int a : que) {
if(a != u)
ok[a] = 0;
}
is[u] = 0;
}
return solve(u);
}
int findEgg(int n, vector<pair<int, int>> edges) {
init(n);
for(int i = 1; i <= n; i++)
ok[i] = 1;
for(const auto &[a, b] : edges)
edge[a].push_back(b), edge[b].push_back(a);
return solve(1);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |