Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector <set <int>> graph;
- int main()
- {
- int n;
- cin >> n;
- int a [n + 1];
- for (int i = 1; i <= n; i++)
- cin >> a [i];
- graph.resize(n + 1);
- for (int i = 1; i <= n; i++)
- {
- if (a [i] != -1)
- graph [a [i]].insert(i);
- }
- int ans = 0;
- int x = 0;
- bool visited [n + 1] = {};
- while (x != n)
- {
- for (int i = 1; i <= n; i++)
- {
- if (!visited [i] && graph [i].empty())
- {
- if (a [i] != -1)
- graph [a [i]].erase(i);
- visited [i] = 1;
- x++;
- }
- }
- ans++;
- }
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment