Advertisement
Josif_tepe

Untitled

Oct 12th, 2023
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <set>
  5. using namespace std;
  6. const int maxn=500000;
  7.  
  8. int main() {
  9.     int n;
  10.     cin >> n;
  11.    
  12.     int nodes_as_trees = 0;
  13.     set<int> st;
  14.     for(int i = 1; i <= n; i++) {
  15.         int x;
  16.         cin >> x;
  17.         if(x == i) {
  18.             nodes_as_trees++;
  19.         }
  20.         else {
  21.             st.insert(x);
  22.         }
  23.     }
  24.     cout << (int) st.size() / 2 + nodes_as_trees << endl;
  25.  
  26.     return 0;
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement