This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Author: Cadocx
Codeforces: https://codeforces.com/profile/Kadoc
VNOJ: oj.vnoi.info/user/Cadoc
*/
#include <bits/stdc++.h>
using namespace std;
// input/output
#define fastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define el cout << '\n'
#define debug(x) cout << #x << " = " << x << '\n'
#define execute cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << "s"
// #pragma GCC optimize("O2", "unroll-loops", "Ofast")
// #pragma GCC target("avx,avx2,fma")
//data type
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define piv pair<int, vector<int>>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return 1; }; return 0; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return 1; }; return 0; }
//STL
#define sz(x) (int)(x).size()
#define FOR(i,l,r) for(auto i = l; i <= r; i++)
#define FORD(i,r,l) for(auto i = r; i >= l; i--)
#define forin(i,a) for(auto i : a)
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
//bitmask
#define bitcnt(n) __builtin_popcount(n)
#define mask(i) (1 << (i))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_on(n, i) ((n) | mask(i))
#define set_off(n, i) ((n) & ~mask(i))
//constant
#define N 1000005
#define MOD 1000230007
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define base 31
#define Kadoc 0
#define Min(u, v) h[u] < h[v]? u:v
int n, d;
int h[N], Node[N], pos[N], Timer = 0;
int rmq[N][33];
int sz[N], del[N], par[N];
int ans[N];
vector<int> g[N];
void dfs(int u, int p = -1){
Node[++Timer] = u;
pos[u] = Timer;
for(int v:g[u]) if(v != p){
h[v] = h[u] + 1;
dfs(v, u);
Node[++Timer] = u;
}
}
void dfs_sz(int u, int p = -1){
sz[u] = 1;
for(int v:g[u]) if(v != p && !del[v]){
dfs_sz(v, u);
sz[u] += sz[v];
}
}
int findcentroid(int u, int p, int n){
for(int v:g[u]) if(v != p && !del[v] && sz[v] > n/2) return findcentroid(v, u, n);
return u;
}
void centroidDecomp(int u, int p = -1){
dfs_sz(u);
u = findcentroid(u, p, sz[u]);
del[u] = 1;
par[u] = p;
for(int v:g[u]) if(v != p && !del[v]) centroidDecomp(v, u);
}
int lca(int u, int v){
int l = pos[u], r = pos[v];
if(l > r) swap(l, r);
int k = 31 - __builtin_clz(r-l+1);
return Min(rmq[l][k], rmq[r-mask(k)+1][k]);
}
int dist(int u, int v){
return h[u] + h[v] - 2*h[lca(u, v)];
}
void upd(int u){
for(int v = u; v > 0; v = par[v]) minimize(ans[v], dist(u, v));
}
int get(int u){
int Ans = INF;
for(int v = u; v > 0; v = par[v]) minimize(Ans, ans[v] + dist(u, v));
return Ans;
}
void solve(){
cin >> n >> d;
FOR(i, 2, n){
int x; cin >> x; x++;
g[i].pb(x);
g[x].pb(i);
}
dfs(1);
centroidDecomp(1);
FOR(i, 1, Timer) rmq[i][0] = Node[i];
for(int j=1; mask(j)<=Timer; ++j){
for(int i=1; i+mask(j)-1<=Timer; ++i){
rmq[i][j] = Min(rmq[i][j-1], rmq[i+mask(j-1)][j-1]);
}
}
vector<pii> v;
FOR(i, 1, n) v.pb({-h[i], i});
sort(all(v));
memset(ans, 0x3f, sizeof ans);
int Ans = 0;
for(auto [depth, u]:v){
if(get(u) >= d){
Ans++;
upd(u);
}
}
cout << Ans;
}
int main(){
#define NAME "TASK"
if(fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
fastIO;
if(Kadoc){
int tc; cin >> tc;
while(tc--){
solve();
}
} else solve();
}
Compilation message (stderr)
catinatree.cpp: In function 'void solve()':
catinatree.cpp:141:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
141 | for(auto [depth, u]:v){
| ^
catinatree.cpp: In function 'int main()':
catinatree.cpp:154:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
154 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
catinatree.cpp:155:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | freopen(NAME".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |