This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 2e5 + 5;
const double eps = 1e-9;
int seg[MAXN*4], st[MAXN], en[MAXN];
int t = 0, depth[MAXN], par[MAXN];
vi adj[MAXN];
vii arr;
void dfs(int u, int p) {
st[u] = t++;
for (int v: adj[u])
if (v != p)
dfs(v, u);
en[u] = t;
}
void upd(int l, int r, int k, int a, int b, int v) {
if (a <= l && r <= b) {
seg[k] = min(seg[k], v);
return;
}
int m = (l + r) / 2;
if (a <= m) upd(l, m, k*2, a, b, v);
if (m < b) upd(m + 1, r, k*2+1, a, b, v);
}
int qry(int l, int r, int k, int a) {
if (a <= l && r <= a) return seg[k];
int m = (l + r) / 2;
if (a <= m) return min(seg[k],qry(l,m,k*2,a));
return min(seg[k],qry(m+1,r,k*2+1,a));
}
int main() {
memset(seg, 0x3f, sizeof seg);
int n, d;
scanf("%d %d", &n, &d);
arr.pb({0, 0});
depth[0] = 0;
for (int i = 1; i < n; i++) {
scanf("%d", &par[i]);
adj[i].pb(par[i]);
adj[par[i]].pb(i);
depth[i] = depth[par[i]] + 1;
arr.pb({depth[i], i});
}
dfs(0, -1);
sort(arr.rbegin(), arr.rend());
int ans = 0;
for (int i = 0; i < n; i++) {
int u = arr[i].se;
if (depth[u] + qry(0, n - 1, 1, st[u]) < d) continue;
int cnt = 0;
for (int v = u; cnt <= d; v = par[v]) {
upd(0, n - 1, 1, st[v], en[v] - 1, depth[u] - 2 * depth[v]);
cnt++;
if (v == 0) break;
}
ans++;
}
printf("%d\n", ans);
}
Compilation message (stderr)
catinatree.cpp: In function 'int main()':
catinatree.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &d);
~~~~~^~~~~~~~~~~~~~~~~
catinatree.cpp:72:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &par[i]);
~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |