Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <queue>
- #include <vector>
- using namespace std;
- vector<int> graph[50001];
- int main() {
- int n;
- cin >> n;
- for(int i = 0; i < n; i++) {
- int x;
- cin >> x;
- x--;
- graph[i].push_back(x);
- }
- vector<bool> visited_bfs(n, false);
- int res = 0;
- for(int i = 0; i < n; i++) {
- if(!visited_bfs[i]) {
- queue<int> q;
- q.push(i);
- vector<bool> visited(n, false);
- visited[i] = true;
- int nodes = 1;
- while(!q.empty()) {
- int node = q.front();
- q.pop();
- for(int j = 0; j < (int) graph[node].size(); j++) {
- int neighbour = graph[node][j];
- if(!visited[neighbour]) {
- visited[neighbour] = true;
- visited_bfs[neighbour] = true;
- q.push(neighbour);
- nodes++;
- res = max(res, nodes);
- }
- }
- }
- }
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement