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>
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
using namespace std;
vector<vector<int>> neigh;
vector<int> dp;
vector<int> visited;
vector<int> center;
int n,d;
pair<int,int> find_furthest(int x, int p){
pair<int,int> furthest={x,1};
for (auto u : neigh[x])
{
if(u==p) continue;
pair<int,int> c=find_furthest(u,x);
c.second++;
if(furthest.second<c.second) furthest=c;
}
dp[x]=furthest.second;
return furthest;
}
bool find_center(int x, int p, int dist, int dest){
if(x==dest) return true;
for (auto u : neigh[x])
{
if(u==p) continue;
bool b=find_center(u,x,dist,dest);
if(b){
if(dp[x]==(dist+1)/2||dp[x]==(2+dist)/2) center.push_back(x);
return true;
}
}
return false;
}
int mark(int x, int p, int res){
int s=(res==0);
int nr=res;
if(nr<=0) nr=d;
for (auto u : neigh[x])
{
if(u==p) continue;
s+=mark(u,x,nr-1);
}
return s;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> d;
dp.resize(n,0);
visited.resize(n,0);
neigh.resize(n);
for (int i = 1; i < n; i++)
{
int p; cin >> p;
neigh[i].push_back(p);
neigh[p].push_back(i);
}
int u=find_furthest(0,-1).first;
pair<int,int> v=find_furthest(u,-1);
find_center(u,-1,v.second,v.first);
int mx=0;
for (int i = 0; i < sz(center); i++)
{
mx=max(mx,mark(center[i],-1,0));
}
cout << mx << "\n";
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... |