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 ...
随机推荐
- PHP面向对象摘要
一.面向对象的三种特性,分别是封装性,继承性和多态性. 1.封装性:封装是面向对象的核心思想,将对象的属性和行为封装起来,不需要让外界知道具体的实现细节,这就是封装思想. 2.继承性:继承性主要是描述 ...
- C#递归查询
一.sql --构造测试数据: 只作演示用 CREATE TABLE [dbo].[Tim_LinqTable]( [Id] int PRIMARY KEY IDENTITY(1,1) NOT NUL ...
- [array] leetCode-4-Median of Two Sorted Arrays-Hard
leetCode-4-Median of Two Sorted Arrays-Hard descrition There are two sorted arrays nums1 and nums2 o ...
- Vue.js—组件快速入门以及实例应用
上次我们学习了Vue.js的基础,并且通过综合的小实例进一步的熟悉了Vue.js的基础应用.今天我们就继续讲讲Vue.js的组件,更加深入的了解Vue,js的使用.首先我们先了解一下什么是Vue.js ...
- ldap数据库--ODSEE--schema
ldap服务器包含上百个对象类型(object class)和属性,这些对象类和属性都可以满足大部分需求,如果你想定义自己的schema,你只能继承扩展现有的schema进行操作. tip: 增加的新 ...
- MyEclipse和Eclipse非常方便的快捷键
1. ctrl+shift+r:打开资源这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如applic*.xml ...
- 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持
HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...
- 关于thinkphp控制器引用model里的方法的一点收获
有时候真的是很绕,为了这一点点收获,我几乎花了一天的时间.当我弄明白了的那一刻,我.........好吧,写到这里,我发现不能换行.好吧,就这样吧,开始说正题:要想在controler从model引用 ...
- 【基础】Attribute的妙用
一.何为Attribute 下面是微软官方对Attribute的解释: 公共语言运行时允许你添加类似关键字的描述声明,叫做Attributes,它对程序中的元素进行标注,如类型.字段.方法和属性等.A ...
- Python之Threading模块
Thread 先引入一个例子: >>> from threading import Thread,currentThread,activeCount >>> > ...