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的更多相关文章

  1. timus 1022 Genealogical Tree(拓扑排序)

    Genealogical Tree Time limit: 1.0 secondMemory limit: 64 MB Background The system of Martians’ blood ...

  2. poj 2367 Genealogical tree

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

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

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

  4. Genealogical tree(拓扑结构+邻接表+优先队列)

    Genealogical tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

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

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

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

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

  7. 【拓扑排序】Genealogical tree

    [POJ2367]Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accep ...

  8. POJ 2367 Genealogical tree【拓扑排序/记录路径】

    Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7101 Accepted: 4585 Spe ...

  9. poj——2367  Genealogical tree

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6025   Accepted: 3969 ...

随机推荐

  1. CSDN 夏令营程序 试题分析 (3)

    首先大家先来看题目: 分析: 三维数组存储以行为主序列,计算公式例如以下: Loc(Ai,j,k)=Loc(Ac1c2c3)+[(i-c1)V2V3+(j-c2)V3+(k-c3)]*L 当中c1.c ...

  2. 使用Nginx+uWSGI+Django方法部署Django程序(下)

    在上一篇文章<五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(上)>中,阐述了如何只使用uWSGI来部署Django程序. 当然,单单只有uWSGI是不够的, ...

  3. Linux——获取IP及其中发现的问题

    由于最近在学习网络编程,喜欢玩IP地址. 首先,我需要搞清楚如何获得本机IP. ===========================================11.04============ ...

  4. nvidia显卡驱动卸载和卸载后的问题

     因为装了nvidia显卡驱动后开机一直处于循环登录界面.password输入正确也是进不去.然后就决定卸载nvidia显卡驱动.安装之后出现还是循环登陆. 是openGL的问题 有至少两种解决方 ...

  5. W25Q128页数和扇区数

    int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size){ *block_size = 4 ...

  6. Centos使用光盘作为本地yum源

    [root@localhost CentOS]# mkdir /media/CentOS把光盘加载到本地[root@localhost CentOS]# mount /dev/cdrom /media ...

  7. Android中图片的三级缓存策略

    在开发过程中,经常会碰到进行请求大量的网络图片的样例.假设处理的不好.非常easy造成oom.对于避免oom的方法,无非就是进行图片的压缩.及时的回收不用的图片.这些看似简单可是处理起来事实上涉及的知 ...

  8. Atitit.编程语言原理---方法重载的实现与设计 调用方法的原理

    Atitit.编程语言原理---方法重载的实现与设计 调用方法的原理 1. 重载包括:普通方法的重载和构造方法的重载 1 1.1. 横向重载”和“纵向重载”1 1.2. 方法签名通过  方法名称,参数 ...

  9. Shift Register

    /*************************************************** /  Shift Register module /  Programing by seong ...

  10. ArrayList和Vector性能对比

    测试条件: 循环次数:1千万次 元素个数:1000个 测试结果: 总结:ArrayList获取元素非常快,不过添加元素没有Vector快,两者各有优势,Vector是线程安全的,而ArrayList是 ...