Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int MAXN = 1e5 + 10;
- vector<int> grafo[MAXN];
- int dp[MAXN];
- int resp = 0;
- void dfs(int v)
- {
- int filhos = 0; int pot = 0;
- for (auto viz : grafo[v])
- {
- dfs(viz);
- if (dp[viz] == 2) {resp++; continue;}
- filhos++;
- pot = max(pot, dp[viz]+1);
- }
- if (filhos >= 3) dp[v] = pot;
- else if (filhos == 2) dp[v] = min(pot, 1);
- else dp[v] = 0;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int N;
- cin >> N;
- for (int i = 2; i <= N; i++)
- {
- int X;
- cin >> X;
- grafo[X].push_back(i);
- }
- dfs(1);
- cout << resp << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement