Advertisement
deushiro

Untitled

Jan 9th, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<int> flag;
  6. //vector<int> prev;
  7. bool check = false;
  8. int lst = -1;
  9. vector<bool> cyc;
  10. vector<pair<int, int>> dp;
  11. vector<vector<int>> a;
  12. vector<bool> used;
  13.  
  14. void dfs(int v){
  15.     flag[v] = 1;
  16.     if(check)
  17.         return;
  18.     for(int u : a[v]){
  19.         if(flag[u] == 0){
  20.             //prev[u] = v;
  21.             dfs(u);
  22.         }
  23.         if(flag[u] == 1){
  24.             lst = u;
  25.             break;
  26.         }
  27.     }
  28.     if(!check)
  29.         flag[v] = 2;
  30. }
  31.  
  32. void cycle(int v){
  33.     cyc[v] = true;
  34.     for(int u : a[v]){
  35.         if(flag[u] == 1)
  36.             cycle(u);
  37.     }
  38. }
  39.  
  40. pair<int, int> tsolve(int v){
  41.     used[v] = true;
  42.     if(a[v].size() == 0){
  43.         pair<int, int> xd = {1, 0};
  44.         return xd;
  45.     }
  46.     int with = 0;
  47.     int without = 0;
  48.     for(int u : a[v]){
  49.         if(!cyc[u] && !used[u]){
  50.             pair<int, int> xd = tsolve(u);
  51.             with += xd.second;
  52.             without += max(xd.first, xd.second);
  53.         }
  54.     }
  55.     pair<int, int> xd = {with, without + 1};
  56.     return(xd);
  57. }
  58.  
  59.  
  60. int main()
  61. {
  62.     int n;
  63.     cin >> n;
  64.     flag.resize(n);
  65.     cyc.resize(n);
  66.     dp.resize(n);
  67.     a.resize(n);
  68.     used.resize(n);
  69.     for(int i = 0; i < n; ++i){
  70.         int x;
  71.         cin >> x;
  72.         x--;
  73.         if(x == -2)
  74.             continue;
  75.         a[i].push_back(x);
  76.         a[x].push_back(i);
  77.     }
  78.     pair<int, int> ans = tsolve(0);
  79.     cout << ans.first << " " << ans.second << endl;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement