# [POJ 2367] Genealogical tree【拓扑序】

## Problem:

Time Limit: 1000MS |
Memory Limit: 65536K |
_{Special Judge} |

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 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 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. Not 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 would define once and for all, an order that would guarantee that every member of the Council takes the floor earlier than each of his descendants.

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 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. Not 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 would define once and for all, an order that would guarantee that every member of the Council 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 Council. According to the centuries-old tradition members of the Council 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 should contain in its only line a sequence of speakers' numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you are 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

Ural State University Internal Contest October'2000 Junior Session

## Solution:

本题除了题目描述有些珂怕之外，其他一切正常，像个**拓扑排序模板题**的样子。

据说拓扑排序有**两种**实现方法，分别是**较好理解**的 Kahn 算法**（Code #1）**和的 DFS 法**实现较简单****（Code #2）**。

个人推荐**前者**。

**Wikipedia:** https://en.wikipedia.org/wiki/Topological_sorting

## Code #1: O(N^{2}) [184K, 0MS]

#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cassert> #include<iostream> #include<algorithm> using namespace std; int N; struct Edge{ int np; Edge *nxt; }; struct Graph{ Edge *V[102], E[10002]; int tope; inline void addedge(int u, int v) {E[++tope].np = v, E[tope].nxt = V[u], V[u] = &E[tope];} } G; int indeg[102]; int deg0[102], topd = 0; int topo[102], topt = 0; int main(){ scanf("%d", &N); memset(indeg, 0, sizeof(indeg)); for(register int i = 1; i <= N; i++){ int v; while(scanf("%d", &v) != EOF && v){ G.addedge(i, v); indeg[v]++; } } for(register int i = 1; i <= N; i++) if(!indeg[i]) deg0[++topd] = i; // Push 0-indegree points into the stack of nodes to be removed while(topd){ topo[++topt] = deg0[topd--]; // Remove a point from the graph for(register Edge *ne = G.V[topo[topt]]; ne; ne = ne->nxt) if(!--indeg[ne->np]) deg0[++topd] = ne->np; // Update the indegrees, and the stack } assert(topt == N); for(register int i = 1; i < topt; i++) printf("%d ", topo[i]); printf("%d\n", topo[topt]); return 0; }

## Code #2: O(N^{2}) [184K, 16MS]

#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cassert> #include<iostream> #include<algorithm> using namespace std; int N; struct Edge{ int np; Edge *nxt; }; struct Graph{ Edge *V[102], E[10002]; int tope; inline void addedge(int u, int v) {E[++tope].np = v, E[tope].nxt = V[u], V[u] = &E[tope];} } G; int vis[102]; // -1: visited, 0: not visited, 1: visiting int topostack[102], topt = 0; inline void toposort_DFS(int u){ vis[u] = 1; for(register Edge *ne = G.V[u]; ne; ne = ne->nxt){ assert(vis[ne->np] != 1); // It must be true unless there're hoops if(!vis[ne->np]) toposort_DFS(ne->np); } vis[u] = -1; topostack[++topt] = u; // The answer is reversed } int main(){ scanf("%d", &N); for(register int i = 1; i <= N; i++){ int v; while(scanf("%d", &v) != EOF && v) G.addedge(i, v); } for(register int i = 1; i <= N; i++) if(!vis[i]) toposort_DFS(i); while(topt > 1) printf("%d ", topostack[topt--]); printf("%d\n", topostack[topt--]); return 0; }

## 发表评论