Advertisement
Guest User

xD

a guest
Mar 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int N,gosc[1000000],stoliki[1000000],wynik;
  4.     void dfs(int x)
  5. {
  6.     stoliki[x]=1;
  7.    if(stoliki[gosc[x]]==0)
  8.     {
  9.         stoliki[gosc[x]]==1;
  10.         dfs(gosc[x]);
  11.     }
  12.  
  13.     }
  14. int main()
  15. {
  16.         cin>>N;
  17.  
  18.            for(int i = 1 ; i <= N ; i++)
  19.                 {
  20.                     cin>>gosc[i];
  21.  
  22.  
  23.  
  24.                 }
  25.  
  26.     for(int i = 1 ; i <= N ; i++)
  27.                 {
  28.                     if(stoliki[i]==0)
  29.                     {
  30.                         wynik++;
  31.                         dfs(i);
  32.                     }
  33.  
  34.  
  35.                 }
  36. cout<<wynik;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement