Genealogical tree
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4420   Accepted: 2933   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行,编号1到n, 每行输入几个数,该行的编号排在这几个数前面,输出一种符合要求的编号名次排序。

此题中的就看测试数据

0

4 5 1 0

1 0

5 3 0

3 0

可知1后面什么也没有,2后面有4,5,1;3后面有1;4后面有5,3;5后面有3;

即上图

#include<cstdio>
#include<iostream>
#include<stack>
#include<cstdlib>
using namespace std;
#define N 101
int cnt,vis[N],du[N],e[N][N],a[N],n,m;
stack<int>s;
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
int x;
while(scanf("%d",&x)==){
if(!x) break;
e[i][x]=;
du[x]++;
}
}
for(int i=;i<=n;i++){
if(!du[i]){
s.push(i);
vis[i]=;
a[++cnt]=i;
}
}
while(!s.empty()){
int p=s.top();
s.pop();
for(int i=;i<=n;i++){
if(e[p][i]){
du[i]--;
}
}
for(int i=;i<=n;i++){
if(!du[i]&&!vis[i]){
s.push(i);
vis[i]=;
a[++cnt]=i;
}
}
}
for(int i=;i<=cnt;i++){
printf("%d ",a[i]);
}
return ;
}
 

poj2367的更多相关文章

  1. [poj2367]Genealogical tree_拓扑排序

    Genealogical tree poj-2367 题目大意:给你一个n个点关系网,求任意一个满足这个关系网的序列,使得前者是后者的上级. 注释:1<=n<=100. 想法:刚刚学习to ...

  2. poj2367 拓扑序

    题意:有一些人他们关系非常复杂,一个人可能有很多后代,现在要制定一种顺序,按照前辈在后代前排列就行 拓扑序裸题,直接建边拓扑排序一下就行了. #include<stdio.h> #incl ...

  3. POJ2367 Genealogical tree (拓扑排序)

    裸拓扑排序. 拓扑排序 用一个队列实现,先把入度为0的点放入队列.然后考虑不断在图中删除队列中的点,每次删除一个点会产生一些新的入度为0的点.把这些点插入队列. 注意:有向无环图 g[] : g[i] ...

  4. POJ2367【拓扑排序】

    很裸的拓扑排序~ //#include <bits/stdc++.h> #include<iostream> #include<string.h> #include ...

  5. 纯拓扑排序一搞poj2367

    /* author: keyboarder time : 2016-05-18 12:21:26 */ #include<cstdio> #include<string.h> ...

  6. POJ2367 拓扑排序 裸题 板子题

    http://poj.org/problem?id=2367 队列版 #include <stdio.h> #include <math.h> #include <str ...

  7. poj2367 Genealogical tree

    思路: 拓扑排序,这里是用染色的dfs实现的.在有环的情况下可以判断出来,没有环的情况下输出拓扑排序序列. 实现: #include <vector> #include <cstri ...

  8. POJ2367(拓扑排序裸题

    #include<iostream> #include<vector> #include<queue> using namespace std; typedef l ...

  9. 拓扑排序 POJ2367Genealogical tree[topo-sort]

    ---恢复内容开始--- Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4875   A ...

随机推荐

  1. vim 注释取消注释多行

    按下ctrl+v选中多行,按大写I(必须是大写的I)进入编辑模式,输入注释符号如#,最后按下esc退出就完成了 取消注释一样按下ctrl+v选中,按d删除

  2. 【PA2012】【BZOJ4289】Tax

    Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值.求从起点1到点N的最小代价. 起点的代价是离开起点的边的边权.终点的代价是进入终点的边的 ...

  3. Spring MVC 解读——<mvc:annotation-driven/>

    Spring MVC 解读——<mvc:annotation-driven/> 一.AnnotationDrivenBeanDefinitionParser 通常如果我们希望通过注解的方式 ...

  4. 解决The prefix 'context' for element 'context:component-scan' is not bound

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  5. mongoDB 删除某一字段、重新名字段

    批量删除字段: {multi:true}:查询到的所有记录都删除. db.collection_name.update({"target_field":{"$exists ...

  6. phpcms前台任意代码执行漏洞(php<5.3)

    phpcms v9 中 string2array()函数使用了eval函数,在多个地方可能造成代码执行漏洞 /phpsso_server/phpcms/libs/functions/global.fu ...

  7. 《学习bash》笔记--进程处理

    1.进程ID和作业编号  当通过附加&号后执行命令时.shell会响应例如以下: $ ls & [1] 3318 当中[1]是作业号,3318是进程号. 一个后台进程完毕时.shell ...

  8. C语言 | 基础知识点笔记

    函数 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...

  9. Oracle 日期总结

    一.带星期格式 .SELECT to_char(to_date('2014-12-12','yyyy-mm-dd'),'MM-DD (DAY)') FROM DUAL .SELECT to_char( ...

  10. 【转】GitHub入门详细讲解

    第一:请登录https://windows.github.com/ 下载您需要的安装软件,进行安装.安装后桌面有:GitHub 和 Git Shell 第二: 申请一个帐号https://github ...