POJ 2367 topological_sort
Genealogical tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2920 Accepted: 1962 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.
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
- /**********************************************
- author : Grant Yuan
- time : 2014.7.29
- algorithm: topological_sort
- source : POJ 2367
- **********************************************/
- #include <iostream>
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<algorithm>
- #define MAX 101
- using namespace std;
- int mat[MAX][MAX],num[MAX];
- int n,m,ans,sum;
- int lu[MAX];
- int main()
- { int a,b;
- while(~scanf("%d",&n)){
- memset(mat,0,sizeof(mat));
- memset(num,0,sizeof(num));
- memset(lu,0,sizeof(lu));
- for(int i=1;i<=n;i++)
- {
- while(1){
- scanf("%d",&a);
- if(a==0) break;
- if(mat[i][a]==0)
- {
- mat[i][a]=1;
- num[i]++;
- }
- }}
- int i,p=n;
- while(1){
- for(i=n;i>0;i--)
- {
- if(num[i]==0)
- break;
- }
- if(i==0)
- break;
- num[i]=-1;
- lu[p--]=i;
- for(int j=1;j<=n;j++)
- {
- if(mat[j][i])
- num[j]--;
- }
- }
- for(int j=1;j<=n;j++)
- if(j<n)printf("%d ",lu[j]);
- else printf("%d",lu[j]);
- printf("\n");
- }
- return 0;
- }
POJ 2367 topological_sort的更多相关文章
- POJ 2367 (裸拓扑排序)
http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
- Poj(2367),拓扑排序
题目链接:http://poj.org/problem?id=2367 题意: 知道一个数n, 然后n行,编号1到n, 每行输入几个数,该行的编号排在这几个数前面,输出一种符合要求的编号名次排序. 拓 ...
- poj 2367 Genealogical tree
题目连接 http://poj.org/problem?id=2367 Genealogical tree Description The system of Martians' blood rela ...
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- 拓扑排序 POJ 2367
今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...
- poj 2367 Genealogical tree (拓扑排序)
火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...
- poj 2367 Genealogical tree【拓扑排序输出可行解】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3674 Accepted: 2445 ...
- POJ 2367 Genealogical tree 拓扑排序入门题
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8003 Accepted: 5184 ...
- POJ 2367:Genealogical tree(拓扑排序模板)
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7285 Accepted: 4704 ...
随机推荐
- arrow functions 箭头函数
ES6里新增加的,与普通方法不同的地方 1.this 的对象在定义函数的时候确定了,而不是在使用的时候才决定 2.不可以使用 new ,也就不能当构造函数 3.this 的值一旦确定无法修改 ...
- Linux学习(十九)软件安装与卸载(二)更换yum源
一.简介 系统自带的源数量有限,而且是国外的源,速度肯定不如国内的.而断网的时候,本地源就可以派得上用处.而RPMForge源是传说中规模最大的一个源.那么接下来我们就来分别配一下本地源,国内源,RP ...
- MQTT——发布报文
发布报文的知识点并不难,只是多.看过前面几章的读者们应该或多或少都认识服务质量QOS.发布报文跟他的联系最紧的.我们也清楚订阅报文里面虽然也有用到QOS,但是他却没有更进一步的联系.往下看就知道是什么 ...
- 深入理解ES6之—set与map
Set是无重复值的有序列表.Set会自动移除重复的值,因此你可以使用它来过滤数组中重复的值并返回结果. Map是有序的键值对,其中的键允许是任何类型. Set和Map是es6新增的两个数据集合. Se ...
- PHP小技巧
1.js获取服务器年月日 var date= '<?php echo date("Y-m-d",time())?>';//获取服务器年月
- oracle数据库表实现主键自增功能
有关oracle中自增序列sequence+触发器trigger:实现数据表TABDATA_LIVE_CYCLE中的主键id的自增. CREATE SEQUENCE TABDATA_LIVE_CYCL ...
- Linux常用配置讲解
本文主要讲解Linux的用户设置.主机名设置.网络配置.防火墙配置 用户传输包的命令lrzsz的安装以及SSH服务配置等基本操作. 1. 用户名设置 服务肯定是为了用户,而用户可能对于Linux并不了 ...
- .net core 依赖注入扩展,实现随处控制反转
在使用.net core时,依赖注入,主要使用通过构造函数注入.小编将通过扩展方式,实现在类中各个地方可以控制反转,获取实例. 1.首先自定义扩展类 using Microsoft.AspNetCor ...
- Spring JDBC 示例
在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常, ...
- linux-之常用命令
Linux常用命令,长时间不用或者想用时具体的使用方法模糊了,可以进行查看,避免还要去其他地方进行查找麻烦,所以找了一些命令进行记录. 1.帮助命令 help 和 man 帮助查看命令的具体使用方 ...