Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #define N int(1e5)
- int from[N], color[N];
- int res, co;
- void dfs (int i)
- {
- if (color[i] == 0)
- color[i] = co;
- else
- {
- if (color[i] == co)
- res++;
- return;
- }
- dfs(from[i]);
- return;
- }
- int main()
- {
- int n, i, j;
- freopen("pigs.in", "r", stdin);
- freopen("pigs.out", "w", stdout);
- scanf("%d", &n);
- for (i = 0; i < n; i++)
- {
- scanf("%d", &j);
- from[i] = j - 1;
- }
- for (i = 0; i < n; i++)
- {
- if (color[i] == 0)
- {
- co++;
- dfs(i);
- }
- }
- printf("%d\n", res);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement