ZhongJiaXuan

[ABC309E] Family and Insurance

Jan 12th, 2024
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. const int N = 3e5 + 10;
  5. vector<int> Edge[N];
  6. int n, m, x, y, ans, now[N];
  7. bool ok[N];
  8. void dfs(int x) {
  9.     if(now[x]) ok[x] = 1;
  10.     for (int i : Edge[x])
  11.         now[i] = max(now[i], now[x] - 1);
  12.     for (int i : Edge[x])
  13.         dfs(i);
  14. }
  15. signed main() {
  16.     ios::sync_with_stdio(false);
  17.     cin.tie(0);
  18.     cin >> n >> m;
  19.     for (int i = 2; i <= n; i++)
  20.         cin >> x, Edge[x].push_back(i);
  21.     for (int i = 1; i <= m; i++)
  22.         cin >> x >> y, now[x] = max(now[x], y + 1);
  23.     dfs(1);
  24.     for (int i = 1; i <= n; i++) ans += ok[i];
  25.     cout << ans;
  26.     return 0;
  27. }
Tags: Atcoder abc309
Advertisement
Add Comment
Please, Sign In to add comment