0%

APCS-小群體

題解

看起來就很DFS

Code

#include <bits/stdc++.h>
using namespace std;

bool vis[100000];
int  fri[100000];
int  cnt = 0;
void dfs(int x,int top, bool m =true){
    //cout << x << " " << top << endl;
    if(x == top && m) {
        cnt ++;
    }

    if(vis[x])return;

    vis[x] = true;
    return dfs(fri[x], top);
}

signed main() {
    int N;cin >> N;
    for(int i=0;i<N;i++)cin >> fri[i];

    for(int i=0;i<N;i++){
        dfs(i,i, false);
    }
    cout << cnt << endl;
    return 0;
}