Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- #define ll long long
- #define ld long double
- #define pb push_back
- #define fe first
- #define se second
- #define st(x) sort(x.begin(),x.end())
- #define rst(x) sort(x.rbegin(),x.rend())
- const long double pi = 3.14159265358979323;
- const double EPS = 1e-12;
- const int N = 1e6 + 5;
- const int mod = 1e9 + 7;
- vector<ll>v[N];
- bool visit[N];
- void dfs(int p)
- {
- visit[p]=1;
- for(int i=0;i<v[p].size();i++)
- {
- if(!visit[v[p][i]])
- {
- dfs(v[p][i]);
- }
- }
- }
- int main()
- {
- fast;
- ll n,a,c=0;
- cin>>n;
- for(int i=1;i<=n;i++){
- cin>>a;
- v[i].pb(a);
- v[a].pb(i);
- }
- for(int i=1;i<=n;i++)
- {
- if(!visit[i]){
- dfs(i);
- c++;
- }
- }
- cout<<c;
- return 0;
- }
Add Comment
Please, Sign In to add comment