Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int long long
- using namespace std;
- const int N = 3e5 + 10;
- vector<int> Edge[N];
- int n, m, x, y, ans, now[N];
- bool ok[N];
- void dfs(int x) {
- if(now[x]) ok[x] = 1;
- for (int i : Edge[x])
- now[i] = max(now[i], now[x] - 1);
- for (int i : Edge[x])
- dfs(i);
- }
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- cin >> n >> m;
- for (int i = 2; i <= n; i++)
- cin >> x, Edge[x].push_back(i);
- for (int i = 1; i <= m; i++)
- cin >> x >> y, now[x] = max(now[x], y + 1);
- dfs(1);
- for (int i = 1; i <= n; i++) ans += ok[i];
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment