POJ 2367 Genealogical tree topological sorting

Source: Internet
Author: User

POJ 2367 Genealogical tree topological sorting

Description

The system of Martians 'blood relations is confusing enough. actually, Martians bud when they want and where they want. they gather together in different groups, so that a Martian can have one parent as well as ten. nobody will be surprised by a hundred of children. martians have got used to this and their style of life seems to them natural.
And in the Planetary councel the confusing genealogical system leads to some embarrassment. there meet the worthiest of Martians, and therefore in order to offend nobody in all of the discussions it is used first to give the floor to the old Martians, than to the younger ones and only than to the most young childless assessors. however, the maintenance of this order really is not a trivial task. no T always Martian knows all of his parents (and there's nothing to tell about his grandparents !). But if by a mistake first speak a grandson and only than his young appearing great-grandfather, this is a real scandal.
Your task is to write a program, which wowould define once and for all, an order that wowould guarantee that every member of the councel takes the floor earlier than each of his descendants.

Input

The first line of the standard input contains an only number N, 1 <= N <= 100-a number of members of the Martian Planetary councer. according to the centuries-old tradition members of the councel are enumerated with the natural numbers from 1 up to N. further, there are exactly N lines, moreover, the I-th line contains a list of I-th member's children. the list of children is a sequence of serial numbers of children in a arbitrary order separated by spaces. the list of children may be empty. the list (even if it is empty) ends with 0.

Output

The standard output shoshould contain its only line a sequence of speakers 'numbers, separated by spaces. if several sequences satisfy the conditions of the problem, you are writing to the standard output any of them. at least one such sequence always exists.

Sample Input

504 5 1 01 05 3 03 0

Sample Output

2 4 5 3 1

Source

Ural State University Internal Contest October '2000 Junior Session

A standard Topology Sorting question.

Key points:

1. Search for vertices without Father's Day. Output These vertices first.

2. Use an array and del [I] records the output points.

3. The output vertex is no longer counted in the Father's Day vertex. cycle Step 1 until all vertices are output.


# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; const int MAX_N = 101; vector
    
     
Pars [MAX_N]; bool del [MAX_N]; void topologicalPrint (int n) {int c = 0; while (c <n) {for (int I = 1; I <= n; I ++) {if (del [I]) continue; // Note: int ps = 0 has been skipped for deleted vertices; for (int j = 0; j <(int) pars [I]. size ()&&! Ps; j ++) {if (! Del [pars [I] [j]) ps ++;} if (! Ps) {del [I] = true; // Delete the printf ("% d", I); c ++ ;}}} int main () {int N, v; while (~ Scanf ("% d", & N) {for (int I = 1; I <= N; I ++) {pars [I]. clear (); del [I] = false ;}for (int I = 1; I <= N; I ++) {scanf ("% d", & v ); while (v) {pars [v]. push_back (I); scanf ("% d", & v) ;}} topologicalPrint (N); putchar ('\ n');} return 0 ;}
    
   
  
 




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.