Z_Michael

1742

Apr 8th, 2020
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector <int> gr, g, d;
  6. int n, cycle, sizecyc, sizeleaf;
  7.  
  8. pair <int, int> rek(int v) {
  9.     if (d[v] == 0) {
  10.         d[v] = 1;
  11.         pair <int, int> u = rek(gr[v]);
  12.         if (u.first == v) {
  13.             cycle++;
  14.             sizecyc += u.second + 1;
  15.         }
  16.         return { u.first, u.second + 1};
  17.     }
  18.     else {
  19.         return { v, 0 };
  20.     }
  21. }
  22.  
  23. int main()
  24. {
  25.     iostream::sync_with_stdio(false);
  26.     cin.tie(0), cout.tie(0);
  27.  
  28.     cin >> n;
  29.     n++;
  30.     gr.resize(n); g.resize(n, 0); d.resize(n, 0);
  31.     for (int i = 1; i < n; i++) {
  32.         int y;
  33.         cin >> y;
  34.         g[y] = 1;
  35.         gr[i] = y;
  36.     }
  37.  
  38.     for (int i = 1; i < n; i++) {
  39.         if (g[i] == 0) {
  40.             rek(i);
  41.             sizeleaf++;
  42.         }
  43.     }
  44.  
  45.     for (int i = 1; i < n; i++) {
  46.         if (d[i] == 0) {
  47.             rek(i);
  48.             sizeleaf++;
  49.         }
  50.     }
  51.  
  52.     cout << sizeleaf << " " << n - 1 - sizecyc + cycle;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment