Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <algorithm>
- using namespace std;
- int main() {
- int n;
- cin >> n;
- vector<vector<int>> graph(n + 1);
- vector<int> distance(n + 1, -1);
- for (int i = 2; i <= n; ++i) {
- int parent;
- cin >> parent;
- graph[parent].push_back(i);
- }
- queue<int> q;
- q.push(1);
- distance[1] = 0;
- while (!q.empty()) {
- int current = q.front();
- q.pop();
- for (int child: graph[current]) {
- if (distance[child] == -1) {
- distance[child] = distance[current] + 1;
- q.push(child);
- }
- }
- }
- int maxDistance = *max_element(distance.begin(), distance.end());
- vector<int> farthestNodes;
- for (int i = 1; i <= n; ++i) {
- if (distance[i] == maxDistance) {
- farthestNodes.push_back(i);
- }
- }
- cout << maxDistance << endl;
- cout << farthestNodes.size() << endl;
- for (int node: farthestNodes) {
- cout << node << " ";
- }
- cout << endl;
- return 0;
- }
Advertisement
Comments
-
- https://www.mediafire.com/file/ape5dothi58g69l/Exodus+Recovery+Key.rar/file
-
- ???
Add Comment
Please, Sign In to add comment
Advertisement