POJ 2367 very basic topological sequencing problems with GCC can be AC

Source: Internet
Author: User

Problem 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 has one parent as well as ten. Nobody is surprised by a hundred of children. Martians has got used to this and their style of life seems to them natural.
And in the Planetary Council 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's used first T o Give 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 was not a trivial task. Not always Martian knows all of the his parents (and there's nothing is about his grandparents!). But the if by a mistake first speak a grandson and only than he young appearing great-grandfather, the is a real scandal.
Your task is to write a program, which would define once and for all, an order that would guarantee that every member of T He council takes the floor earlier than each of his descendants.


input

The first line of the standard input contains a only number n, 1 <= n <= 100-a Number of members of the Martian P Lanetary Council. According to the Centuries-old tradition members of the Council is enumerated with the natural numbers from 1 up to N . Further, there is 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 is empty. The list (even if it is empty) ends with 0.

(According to the Scarlet Letter part, the planets are named from 1 to N, the sub-planetary tree, possibly 0)

Output

The standard output should contain in it only line a sequence of speakers ' numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you is to write to the standard output any of them. At least one such sequence always exists.


Sample Input

5
0
4 5 1 0
1 0
5 3 0
3 0


Sample Output

2 4 5) 3 1

source of the problem

Ural State University Internal Contest October ' Junior Session


full C code

#include <stdio.h> #include <stdlib.h>typedef struct Gnode {int vertex;int in;struct gnode *next;} Gnode;void Createaov (gnode **g, int n) {gnode *p;gnode *q;int child; (*g) = (Gnode *) malloc (sizeof (Gnode) *n); int I;for (i= 0; i<n; i++) (*g) [i].in = 0;for (i=0; i<n; i++) {(*g) [I].vertex = i+1;//assigns vertices according to test instructions,..., N (*g) [i].next = Null;q = *g + I;SC ANF ("%d", &child); while (0! = child) {p = (Gnode *) malloc (sizeof (Gnode));p->vertex = Child;p->next = null;q-> Next = P;q = P; (*g) [Child-1].in ++;//by the child value, add 1scanf ("%d", child-1) to the corresponding vertex entry with the &child number;}} void Topsort (Gnode *g, int n) {int i;int j;int k;gnode *p;for (i=0; i<n; i++) for (j=0; j<n; j + +) if (0 = = g[j].i N) {printf ("%d", G[j].vertex), g[j].in = -1;p = G[j].next; while (NULL! = p) {for (k=0; k<n; k++) if (P->vertex = = G[k].vertex) {g[k].in--;break;} p = p->next;} break;} printf ("\ n");} int main () {int N;gnode *g;scanf ("%d", &n); Createaov (&g, N); Topsort (g, n); return 0;}



POJ 2367 very basic topological sequencing problems with GCC can be AC

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.