Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int N,gosc[1000000],stoliki[1000000],wynik;
- void dfs(int x)
- {
- stoliki[x]=1;
- if(stoliki[gosc[x]]==0)
- {
- stoliki[gosc[x]]==1;
- dfs(gosc[x]);
- }
- }
- int main()
- {
- cin>>N;
- for(int i = 1 ; i <= N ; i++)
- {
- cin>>gosc[i];
- }
- for(int i = 1 ; i <= N ; i++)
- {
- if(stoliki[i]==0)
- {
- wynik++;
- dfs(i);
- }
- }
- cout<<wynik;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement