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. gRPC-go 入门(1):Hello World

    摘要 在这篇文章中,主要是跟你介绍一下gRPC这个东西. 然后,我会创建一个简单的练习项目,作为gRPC的Hello World项目. 在这个项目中,只有很简单的一个RPC函数,用于说明gRPC的工作 ...

  2. burp suite之spider(爬虫)

    spider (蜘蛛,这里的意思指爬行) 像蜘蛛一样在网站上爬行出网站的个个目录信息,并发送至Target. 1.Control(控制) Spider is paused :停止蜘蛛爬行 Clear ...

  3. 摄像头Sensor 图像格式

    以0V7725为例: 顺便介绍一下0V7725的主要管脚,管脚定义能体现功能,体现使用方法.

  4. Java 集合 | 红黑树 | 前置知识

    一.前言 0tnv1e.png 为啥要学红黑树吖? 因为笔者最近在赶项目的时候,不忘抽出时间来复习 Java 基础知识,现在准备看集合的源码啦啦.听闻,HashMap 在 jdk 1.8 的时候,底层 ...

  5. 手把手教你AspNetCore WebApi:Serilog(日志)

    前言 小明目前已经把"待办事项"功能实现了,API文档也搞定了,但是马老板说过,绝对不能让没有任何监控的项目上线的. Serilog是什么? 在.NET使用日志框架第一时间会想到N ...

  6. Dockerize ASP。净样板项目

    Get the source code from the Github repository. 介绍 在这篇文章中,我将一步步地向你展示如何在Docker上运行ABP模块零核心模板.然后,我们将讨论其 ...

  7. Linux网卡命名规则

    网卡命名 一.为什么需要这个      服务器通常有多块网卡,有板载集成的,同时也有插在PCIe插槽的.Linux系统的命名原来是eth0,eth1这样的形式,但是这个编号往往不一定准确对应网卡接口的 ...

  8. git 本地回滚到上一个版本

    linux下是 git reset --hard HEAD^ windows下是 git reset --hard HEAD"^"

  9. 实验 4:Open vSwitch 实验——Mininet 中使用 OVS 命令

    一.实验目的 Mininet 安装之后,会连带安装 Open vSwitch,可以直接通过 Python 脚本调用Open vSwitch 命令,从而直接控制 Open vSwitch,通过实验了解调 ...

  10. WPF开源控件扩展库 - MaterialDesignExtensions

    Material Design Extensions 在WPF开源控件库 Material Design in XAML Toolkit(本站介绍:链接)的基础上进行了控件扩展和特性新增.本开源项目中 ...