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.

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
/*
拓扑排序 :由某个集合上的一个偏序打得到该集合上的一个全序。
直观的说,偏序指集合中仅有布冯成员可比较,全序则指集合中全部成员之间都可以比较 。
对有向图进行拓扑排序:
1)在有向图中选一个没有前驱的定点且输出
2)从图中伤处该顶点和所有以他结尾的弧
*/
#include<stdio.h>
# define N 200
int indegree[N];
//统计每个节点的入度,通过图的邻接矩阵 ,如果两个节点有关系 ,对应位置的数值就是 1
void Findindegree(int (*y)[N],int c){
int i,j;
for(i=1;i<=c;i++) indegree[i]=0;
for(i=1;i<=c;i++){
for(j=1;j<=c;j++){
indegree[j]=indegree[j]+y[i][j];
}
}
// for(i=1;i<=c;i++) printf("=%d=",indegree[i]);
// printf("\n");
}
int TopSort(int c,int (*y)[N]){
int o,p,count,x=0,k;
int z[N]={0};
//入度为零就进栈,这里用数组表示栈
for(o=1;o<=c;o++){
if(indegree[o]==0){
z[x]=o;
x++;
// printf("-----%d-----%d\n",x,z[x]);
}
}
count = 0; //统计输出的定点
while(x!=0){
// for(o=1;o<=c;o++) printf("---%d---",indegree[o]);
o=z[--x];
printf(" %d ",o); //输出栈顶元素
// printf("=-----%d-----=\n \n",o);
count++;
//对输出的元素的子节点的入度进行更新,同时判断入度为零就进栈
for(k=1;k<=c;k++){
if(y[o][k]){
indegree[k]--;
if(indegree[k]==0) z[x++] = k;
// printf("---22222--%d-----%d\n",x,z[x]);
} }
}
if(count < c) return 0;
else return 1;
}
int main()
{
int i,j,a,b,sum;
int x[N][N]={{0}};
scanf("%d",&a); //数据量
for(i=1;i<=a;i++){
do{ //获取数据,以 0 结束
scanf("%d",&b);
if(b==0) break;
x[i][b] = 1 ;
}while(1);
}
/* for(i=1;i<=a;i++){
for(j=1;j<=a;j++){
printf("= %d =",x[i][j]);
}
printf("\n");
}*/
Findindegree(x,a);
sum=TopSort(a,x);
return 0;
}

  

  

puk2367 拓扑排序的更多相关文章

  1. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  2. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  3. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  4. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  5. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  6. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  7. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

  8. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

  9. *HDU1285 拓扑排序

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. PHP序列化与反序列化学习

    序列化与反序列化学习 把对象转换为字节序列的过程称为对象的序列化:把字节序列恢复为对象的过程称为对象的反序列化. <?php class UserInfo { public $name = &q ...

  2. windows10开机后恢复关机前打开的chrome网页

    开始-设置-账户-登录选项-隐私,下面一个"更新或重启后--"即第二个开关打开. 这时,无论是重启.更新重启或关机后开机,均可恢复原关机时的网页(和应用). 注:题主安装的wind ...

  3. Java知识系统回顾整理01基础04操作符01算术操作符

    一.算数操作符类别 基本的有: + - * / % 自增 自减: ++ -- 二.基本算数操作符 + - * / 基本的加 减 乘 除 public class HelloWorld { public ...

  4. c语言 static的用法

    static在c里面可以用来修饰变量,也可以用来修饰函数.先看用来修饰变量的时候.变量在c里面可分为存在全局数据区.栈和堆里.其实我们平时所说的堆栈是栈而不是堆,不要弄混.int a ;int mai ...

  5. CF471D MUH and Cube Walls

    Link 一句话题意: 给两堵墙.问 \(a\) 墙中与 \(b\) 墙顶部形状相同的区间有多少个. 这生草翻译不想多说了. 我们先来转化一下问题.对于一堵墙他的向下延伸的高度,我们是不用管的. 我们 ...

  6. 【3】Java面试-Servlet

    Servlet面试问题 Q1.什么是servlet? Java Servlet是服务器端技术,通过提供对动态响应和数据持久性的支持来扩展Web服务器的功能. javax.servlet和javax.s ...

  7. JVM 第四篇:可视化 JVM 故障处理工具

    本文内容过于硬核,建议有 Java 相关经验人士阅读. 1. 可视化工具 在 JDK 中为我们提供了大量的 JVM 故障处理工具,都在 JDK 的 bin 目录下: 这其中除了大量的命令行工具以外,还 ...

  8. # vue 如何通过前端来导出excel表格

    在做一些简单的demo时,偶尔会遇到导出excel表格.如果请后端帮忙的话 比较浪费时间,那么前端如何导出excel表格,下面就来记录一下之前使用到的案例 一.安装依赖 npm i file-save ...

  9. 洛谷 P1903 [国家集训队]数颜色 / 维护队列 带修莫队

    题目描述 墨墨购买了一套\(N\)支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: \(1\). \(Q\) \(L\) \(R\)代表询问你从第\(L\) ...

  10. UIScrollView无法滚动以及超出屏幕的内容无法进行编辑

    通过UITextView实现简单的富文本内容编辑,功能按钮包装时遇到些问题,简单记录如下 1.通过UIToolbar将字体设置功能罗列出来,由于功能过多,通过UIScrollView进行滑动控制 1) ...