Genealogical tree
Genealogical tree
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6032 Accepted: 3973 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
//题意: 求出任意一个 1 -- n 的拓扑排列,第一行是 n ,然后 i 行,每行一些数 x ,代表 i 要求在 x 前,0代表结束该行输入
//拓扑排序模板题
DFS
# include <cstring>
# include <cstdio>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N = ;
/**************************/ int n;
bool G[N][N];
int ans[N];
int vis[N];
int dex; bool dfs(int u)
{
vis[u]=-; //标记为正在遍历的
for (int v=;v<=n;v++)
{
if (G[u][v])
{
if (vis[v]==-) return ; //说明组成了环,不能拓扑排序
else if (!vis[v]&&!dfs(v)) return ; //继续遍历未遍历的
}
}
vis[u]=; //标记遍历过了
ans[dex--]=u; //输出的就是这个数组
return ;
} bool toposort()
{
dex=n;
for (int i=;i<=n;i++) //将所有数都遍历
if (!vis[i]&&!dfs(i)) return ;
return ;
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
memset(G,,sizeof (G));
memset(vis,,sizeof(vis));
for (int i=;i<=n;i++)
{
int x;
while ()
{
x = scan();
if (x==) break;
G[i][x]=;
}
}
if (toposort())
{
for (int i=;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
}
return ;
}
Genealogical tree的更多相关文章
- timus 1022 Genealogical Tree(拓扑排序)
Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ...
- poj 2367 Genealogical tree
题目连接 http://poj.org/problem?id=2367 Genealogical tree Description The system of Martians' blood rela ...
- poj 2367 Genealogical tree【拓扑排序输出可行解】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3674 Accepted: 2445 ...
- Genealogical tree(拓扑结构+邻接表+优先队列)
Genealogical tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- 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 ...
- 【拓扑排序】Genealogical tree
[POJ2367]Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accep ...
- POJ 2367 Genealogical tree【拓扑排序/记录路径】
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7101 Accepted: 4585 Spe ...
- poj——2367 Genealogical tree
Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6025 Accepted: 3969 ...
随机推荐
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-人机界面如何快速调整大量控件的位置
打开元素列表,然后直接从顶部按住Shift批量选中控件即可 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的 ...
- 使用Vmware虚拟机部署Swift开发环境之Mac OSX系统安装
一.使用VMware虚拟机部署Swift开发环境所需工具: Vmware Workstation 10.0虚拟机软件VM安装Mac解锁工具Unlock苹果操作系统(Mac OS X Mavericks ...
- 解决apt-get的E: Could not get lock /var/lib/dpkg/lock方法
使用apt-get进行软件的install或update时,有时会出现以下提示信息: E: Could not get lock /var/lib/dpkg/lock - open (11 Resou ...
- 使用JDK自带jvisualvm监控tomcat(收藏)
发表于2年前(2013-08-27 16:28) 阅读(11467) | 评论(14) 326人收藏此文章, 我要收藏 赞9 阿里云携手开源中国众包平台发布百万悬赏项目 » jvisualvm ...
- 开放平台(接口)开发-1-天气API接口大全
前几天有个公司让准备一下第二次面试.应聘的是IOS开发实习生,可是之前一直做android,IOS刚接触了一个月,会的不是非常多,所以决定做一个实际的项目展现给面试官,余同学给了个建议:能够做一 ...
- STM32在程序运行过程中关闭定时器重新打开后定时器不工作的问题
问题:数码管显示程序放在定时TIM2中断函数里面扫描,想要实现在关闭某一功能的时候数码管不显示. 刚开始的想法是开关关闭,关闭定时器时钟:开关打开,打开定时器时钟:(但实验证明再次打开开关时定时器2却 ...
- Atitit.数据库分区的设计 attilax 总结
Atitit.数据库分区的设计 attilax 总结 1. 分区就是分门别类的文件夹 (what)1 2. 分区的好处(y)1 3. 分区原则(要不要分区,何时分区)how2 4. 主要的分表类型有 ...
- Atitit.upnp SSDP 查找nas的原理与实现java php c#.net c++
Atitit.upnp SSDP 查找nas的原理与实现java php c#.net c++ 1. 查找nas的原理1 2. 与dlna的关系1 3. 与ssdp的关系1 4. Cling - Ja ...
- 11.static(转)
本文转自:http://blog.csdn.net/keyeagle/article/details/6708077 google了近三页的关于C语言中static的内容,发现可用的信息很少,要么长篇 ...
- python C PyObject
#include"Python.h" //three ways : /* PyObject *MyFunction(PyObject *self, PyObject *args); ...