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"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 2e5+3, oo = 1e9;
int choice[mxN],d[mxN],N,D, minput[mxN];
vi adj[mxN], ans;
void dfs(int at, int from) {
choice[at]=at;
minput[at]=oo;
for(int to : adj[at]) if(to!=from) {
d[to] = d[at]+1;
dfs(to,at);
minput[at] = min(minput[at],minput[to]+1);
if(choice[to]) {
if((d[choice[to]]-d[at])*2>=D) {
ans.push_back(choice[to]);
minput[at] = min(minput[at], d[choice[to]]-d[at]);
} else if(d[choice[to]]>d[choice[at]]) {
choice[at] = choice[to];
}
}
}
int got = d[choice[at]]-d[at];
if(got+minput[at]<D) {
choice[at]=0;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> D;
for(int i=2;i<=N;++i) {
int x; cin >> x;
adj[x+1].push_back(i);
}
dfs(1,0);
if(choice[1]) ans.push_back(choice[1]);
cout << ans.size() << '\n'; //<< 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... |