Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Make CSP great again
- #include <bits/stdc++.h>
- #define TASK "TESTCODE"
- using namespace std;
- const int N = 1e5;
- int a[N + 1], n, res = 0, b[N + 1];
- void read()
- {
- cin >> n;
- for (int i = 1; i <= n; ++ i)
- {
- cin >> a[i];
- }
- }
- bool DFS(int u)
- {
- if (b[u] == 1)
- {
- return false;
- }
- if (b[u] == 2)
- {
- return true;
- }
- b[u] = 2;
- return DFS(a[u]);
- }
- void reDFS(int u)
- {
- b[u] = 1;
- if (b[a[u]] == 2)
- {
- reDFS(a[u]);
- }
- }
- void solve()
- {
- for (int i = 1; i <= n; ++ i)
- {
- if (DFS(i))
- {
- ++res;
- }
- reDFS(i);
- }
- cout << res;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- //freopen(TASK".INP", "r", stdin);
- //freopen(TASK".OUT", "w", stdout);
- int t = 1;
- bool typetest = false;
- if (typetest)
- {
- cin >> t;
- }
- for (int __ = 1; __ <= t; ++ __)
- {
- read();
- solve();
- }
- }
Add Comment
Please, Sign In to add comment