이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
cerr<<vars<<" = ";
string delim="";
(...,(cerr<<delim<<values,delim=", "));
cerr<<"\n";
}
#else
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {}
#endif
#define pb push_back
#define sz(x) (int)(x.size())
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> inline void maxa(T& a,T b){a=max(a,b);}
template<typename T> inline void mina(T& a,T b){a=min(a,b);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mxN=2e5+5; //make sure this is right
const int mod=1e9+7;
vector<int> graph[mxN],path;
int bad[mxN],ht[mxN],seen[mxN];
priority_queue<pii> q;
void dfs(int at,int p=-1){
for(int &i:graph[at]){
if(i==p)
continue;
ht[i]=ht[at]+1;
dfs(i,at);
}
q.push({ht[at],at});
}
void paint(int at,int d,int dep){
if(d==0 || ht[at]>dep || seen[at])
return;
bad[at]=1;
seen[at]=1;
path.pb(at);
for(int &i:graph[at])
paint(i,d-1,dep);
}
int main(){
cin.sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int n,d; cin>>n>>d;
for(int i=1;i<n;i++){
int x; cin>>x;
graph[x].pb(i);
graph[i].pb(x);
}
dfs(0);
int ans=0;
while(!q.empty()){
int at=q.top().second; q.pop();
if(bad[at])
continue;
ans++;
paint(at,d,ht[at]);
while(sz(path)){
seen[path.back()]=0;
path.pop_back();
}
}
cout<<ans<<"\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... |