이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int MAXK = 21;
set<pair<int, int> > verts;
array<vector<int>, MAXN> grafo;
array<int, MAXN> Prof;
array<set<pair<int, int> >, MAXN> active;
// Prof id
int dp[MAXN][MAXK];
int N, D;
int tmp = 0;
void dfs(int v, int p)
{
Prof[v] = Prof[p]+1;
active[v].emplace(Prof[v], v);
for (auto viz : grafo[v])
{
if (viz == p) continue;
dfs(viz, v);
set<pair<int, int> > temp;
if (active[viz].size() > active[v].size()) swap(active[v], active[viz]);
for (auto [prof, cur] : active[viz])
{
if (active[v].empty()) {temp.emplace(prof, cur); continue;}
auto lastPar = active[v].lower_bound(make_pair(prof, -1));
if (lastPar == active[v].end())
{
if (prof + active[v].rbegin()->first - 2*Prof[v] + 1 <= D) active[v].clear();
temp.emplace(prof, cur);
continue;
}
int lastProf = lastPar->first;
if (prof + lastProf - 2*Prof[v] + 1 <= D) continue;
for (auto it = active[v].begin(); it != lastPar && prof + it->first - 2*Prof[v] + 1 <= D; it = active[v].erase(it));
temp.emplace(prof, cur);
}
for (auto x : temp) active[v].insert(x);
temp.clear();
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> D;
for (int i = 1; i < N; i++)
{
int X;
cin >> X;
grafo[X].push_back(i);
grafo[i].push_back(X);
}
// for (int k = 0; k < MAXK; k++) dp[N][k] = N;
dfs(0, 0);
cout << active[0].size() << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |