이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
using i64 = long long;
#define N 200005
int n, d, l[N], h[N], alloc = 1, dp[N][2], t[N];
pair<int, int> g[N<<1];
void upd(int p, int k) { for (; p ; p-=p&-p) t[p] += k; }
int qry(int p) { int z = 0; for (; p < N; p+=p&-p) z += t[p]; return z; }
void add_edge(int u, int v)
{
int p = alloc++;
g[p] = {v, h[u]};
h[u] = p;
}
void dfs(int u, int p = -1, int dep = 1)
{
l[u] = dep;
int base = 0;
for (auto j = h[u]; j; j = g[u].second)
{
int v = g[u].first;
if (v == p) continue;
dfs(v, u, dep+1);
base += dp[v][0] - 1;
upd(dp[v][1], 1);
}
dp[u][0] = base + 1, dp[u][1] = l[u];
for (auto j = h[u]; j; j = g[u].second)
{
int v = g[u].first;
if (v == p) continue;
int sel = base + 1 + qry(d - (dp[v][1] - dep) + dep);
if (dp[u][0] < sel)
{
dp[u][0] = sel;
dp[u][1] = dp[v][1];
}
else if (dp[u][0] == sel && dp[v][1] > dp[u][1])
{
dp[u][1] = dp[v][1];
}
}
for (auto j = h[u]; j; j = g[u].second)
{
int v = g[u].first;
if (v == p) continue;
upd(dp[v][1], -1);
}
}
int main()
{
ShinLena;
cin >> n >> d;
for (int i = 1, v; i < n; ++i) cin >> v, add_edge(i, v), add_edge(v, i);
dfs(1);
cout << dp[1][1];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |