Our crew (BFS good question)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.