Our crew (25 min)
Given the genealogy of a large family, ask you to give a list of the youngest generation.
Input Format:
The input gives the total number of family population N (positive integers not exceeding 100 000) in the first row--for simplicity, we numbered the family members from 1 to N. The second line then gives the N number, where the I number corresponds to the parent of the I-bit member. The parent number of the generational highest ancestor in the family tree is-1. The numbers in a row are separated by a space.
output Format:
First output the smallest generational (the ancestor's generation is divided into 1, the following increments). The number of the smallest member of the generational is then output in ascending order on the second line. The numbers are separated by a space, and no extra spaces are allowed at the end of the line.
Input Sample:
92 6 5 5 -1 5 6 4 7
Sample output:
41 9
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5#include <queue>6#include <algorithm>7 using namespacestd;8 Const intmaxn=1e5+Ten;9vector<int>V[MAXN];Tenvector<int>F[MAXN]; One intn,x; A intHigh ; - intparent; - voidBFS () the { -queue<int>Q; - Q.push (parent); -High=0; + F[high].push_back (parent); - + while(!q.empty ()) A { at - intsum=q.size (); -high++; - for(intI=0; i<sum;i++) - { - intHead=Q.front (); in Q.pop (); - for(intj=0; J<v[head].size (); j + +) to { + F[high].push_back (V[head][j]); - Q.push (V[head][j]); the } * $ }Panax Notoginseng } - } the intMain () + { ACin>>N; the for(intI=1; i<=n;i++) + { -scanf"%d",&x); $ if(x>0) $ V[x].push_back (i); - Else -Parent=i; the } - BFS ();Wuyicout<Endl; the for(intI=0; i<f[high-1].size (); i++) - { Wu if(i!=0) -printf" "); About $printf"%d", f[high-1][i]); - } -cout<<Endl; - return 0; A}
Our crew (BFS good question)