Advertisement
ThegeekKnight16

Ink Colors

Jun 6th, 2022
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 1e5 + 10;
  4. vector<int> grafo[MAXN];
  5. int dp[MAXN];
  6. int resp = 0;
  7.  
  8. void dfs(int v)
  9. {
  10.     int filhos = 0; int pot = 0;
  11.     for (auto viz : grafo[v])
  12.     {
  13.         dfs(viz);
  14.         if (dp[viz] == 2) {resp++; continue;}
  15.         filhos++;
  16.         pot = max(pot, dp[viz]+1);
  17.     }
  18.     if (filhos >= 3) dp[v] = pot;
  19.     else if (filhos == 2) dp[v] = min(pot, 1);
  20.     else dp[v] = 0;
  21. }
  22.  
  23. int main()
  24. {
  25.     ios_base::sync_with_stdio(false);
  26.     cin.tie(NULL);
  27.     int N;
  28.     cin >> N;
  29.     for (int i = 2; i <= N; i++)
  30.     {
  31.         int X;
  32.         cin >> X;
  33.         grafo[X].push_back(i);
  34.     }
  35.     dfs(1);
  36.     cout << resp << '\n';
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement