Genealogical tree
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3658   Accepted: 2433   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

 
题意:
 
        首先输入一个数n,代表人数,然后输入n行,每一行的输入分别代表他们自己的后代(以第i行为例:第i 行输入的是第i个人的后代),然后通过n行的输入。来确定谁最大,谁第二大,一直将全部的人都输出!(依照年龄由大到小的输出),看不懂题就绘图,一绘图就知道了!
 
代码:
 
//理解题意非常重要!
#include <stdio.h>
#include <string.h>
int n;
int map[105][105];
int indegree[105];
int queue[105];
void topo()
{
int m,t=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(indegree[j]==0)
{
m=j;
break;
}
}
indegree[m]=-1;
queue[t++]=m;
for(int j=1;j<=n;j++)
{
if(map[m][j]==1)
{
indegree[j]--;
}
}
}
for(int i=0;i<n-1;i++)
printf("%d ",queue[i]);
printf("%d\n",queue[n-1]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(map,0,sizeof(map));
memset(indegree,0,sizeof(indegree));
int a,b;
for(int i=1;i<=n;i++)
{
while(scanf("%d",&a)&&a)//注意输入的格式,每一行遇到0就结束!
{
if(map[i][a]==0)
{
map[i][a]=1;
indegree[a]++;
}
}
}
topo();
}
return 0;
}

poj 2367的更多相关文章

  1. POJ 2367 topological_sort

    Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2920 Accepted: 1962 Spe ...

  2. POJ 2367 (裸拓扑排序)

    http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...

  3. Poj(2367),拓扑排序

    题目链接:http://poj.org/problem?id=2367 题意: 知道一个数n, 然后n行,编号1到n, 每行输入几个数,该行的编号排在这几个数前面,输出一种符合要求的编号名次排序. 拓 ...

  4. poj 2367 Genealogical tree

    题目连接 http://poj.org/problem?id=2367 Genealogical tree Description The system of Martians' blood rela ...

  5. 图论之拓扑排序 poj 2367 Genealogical tree

    题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...

  6. 拓扑排序 POJ 2367

    今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...

  7. poj 2367 Genealogical tree (拓扑排序)

    火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第 ...

  8. poj 2367 Genealogical tree【拓扑排序输出可行解】

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3674   Accepted: 2445 ...

  9. POJ 2367 Genealogical tree 拓扑排序入门题

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8003   Accepted: 5184 ...

  10. POJ 2367:Genealogical tree(拓扑排序模板)

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7285   Accepted: 4704 ...

随机推荐

  1. bzoj 1067: [SCOI2007]降雨量

    题目链接: bzoj 1067: [SCOI2007]降雨量 题解: 很简单的一道题,但代码里有许多细节需要注意,切容易出错,调了三个小时OTZ 做一个st表维护区间最大值就 在获得年份在序列中的po ...

  2. luogu P1577 切绳子

    题目描述 有N条绳子,它们的长度分别为Li.如果从它们中切割出K条长度相同的 绳子,这K条绳子每条最长能有多长?答案保留到小数点后2位. 输入输出格式 输入格式: 第一行两个整数N和K,接下来N行,描 ...

  3. 解魔方的机器人攻略13 – 安装Lejos(上)

    由 动力老男孩 发表于 2009/12/27 16:58:23 Firmware(固件)相当于是机器人的操作系统,乐高NXT出厂时已经内置了一套Firmware,并且配备了非常强大的LabVIEW开发 ...

  4. 提高MSSQL数据库性能(1)对比count(*) 和 替代count(*)

    原文:提高MSSQL数据库性能(1)对比count(*) 和 替代count(*) 文章准备的数据库: Atricles 表   数据量60690000条数据 ArticleID 主键自增列+自动建立 ...

  5. 【spring boot】集成了druid后,同样的mybatis模糊查询语句出错Caused by: com.alibaba.druid.sql.parser.ParserException: syntax error, error in :'name LIKE '%' ? '%'

    druid版本是 <!-- https://mvnrepository.com/artifact/com.alibaba/druid 数据库连接池--> <dependency> ...

  6. Javascript 内置值、typeof运算符、true/false判断

    一.内置值       true       false         null         undefined       NaN       Infinity   二.typeof运算结果 ...

  7. Unicode类别

    Unicode 通用类别: http://msdn.microsoft.com/zh-cn/library/20bw873z(VS.80).aspx 类别 说明 Lu 字母,大写 Ll 字母,小写 L ...

  8. linux fork的缺点

    Disadvantage of fork linux环境下, JBoss中调用curl下载文件,  发现curl占用的内存和JBoss一样多. Historical Background and Pr ...

  9. Linux学习之二-Linux系统的目录结构

    Linux学习之二-Linux系统的目录结构 在Linux的根目录下,有很多的目录,但是需要记住,对于Linux而言,一切皆文件.因此此处的目录也是文件.用ls / 命令就能看到根目录下的各类不同的目 ...

  10. Node.js 使用http客户端向网站请求数据并保存

    app.js代码: // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // 内置文件处理模块 var fs=requir ...