题目连接

http://poj.org/problem?id=2367

Genealogical tree

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

拓扑排序。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::map;
using std::min;
using std::find;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 110;
const int INF = 0x3f3f3f3f;
struct TopSort {
struct edge { int to, next; }G[(N * N) << 1];
int tot, inq[N], head[N], topNum[N];
inline void init() {
tot = 0, cls(inq, 0), cls(head, -1);
}
inline void add_edge(int u, int v) {
G[tot].to = v; G[tot].next = head[u]; head[u] = tot++;
}
inline void built(int n) {
int v;
for(int i = 1; i <= n; i++) {
while(~scanf("%d", &v), v) {
inq[v]++, add_edge(i, v);
}
}
}
inline void bfs(int n) {
int k = 0;
queue<int> q;
rep(i, n) {
if(!inq[i + 1]) {
q.push(i + 1);
}
}
while(!q.empty()) {
int u = q.front(); q.pop();
topNum[k++] = u;
for(int i = head[u]; ~i; i = G[i].next) {
if(--inq[G[i].to] == 0) {
q.push(G[i].to);
}
}
}
rep(i, k) printf("%d%c", topNum[i], i < k - 1 ? ' ' : '\n');
}
inline void solve(int n) {
init(), built(n), bfs(n);
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
while(~scanf("%d", &n)) {
go.solve(n);
}
return 0;
}

poj 2367 Genealogical tree的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. Poj 2367 Genealogical tree(拓扑排序)

    题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...

  7. POJ 2367 Genealogical tree 拓扑题解

    一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就非常方便能够查找到那些点是没有单亲的节点,那么就能够输出该节点了. 详细实现的方法有非常多种的,比方记录每一个节点的入度 ...

  8. POJ 2367 Genealogical tree【拓扑排序】

    题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 ...

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

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

随机推荐

  1. 8051学习笔记——AD

    AD.C #include<reg52.h> #include <iic.h> #define PCF8591 0x90 //PCF8591 地址 sbit LS138A=P2 ...

  2. EXTJS 密码确认与验证

    extjs 框架是一个非常优秀的前端框架,提供了丰富的功能与炫丽的界面展示,在用 extjs 创建表单时,特别是在注册或修改密码的时候,要对密码进行确认,这里就密码确认一致性验证和大家分享自己的心得与 ...

  3. 由多次使用Statement实例引起的Result set already closed异常的解决方案

    在不同版本的Weblogic平台上迁移应用,产生了很严重的JDBC驱动版本不兼容的问题. 但是归根究底是代码的问题,废话少说,上代码示例. ..... //以下是问题代码 ResultSet rs=n ...

  4. 实时显示GetLastError值

    在Watch窗口中输入$err,hr 自动显示上个函数返回值

  5. 分层服务提供者(LSP)

    分层服务提供者(LSP)(1) 开发过滤数据包的LSP程序可以定义过滤规则,恩,先看看LSP本身是DLL,可以将它安装至Winsock目录,创建套接字的应用程序不必知道此LSP的任何信息就能调用它 1 ...

  6. JNI基础概念以及原理-2016.01.11

    Java到C数据类型转换 1 基础类型 Java与Jni类型对应关系 2 String到char数组 具体使用方式 JNIEXPORT jstring JNICALL Java_com_zhoulee ...

  7. ECSHOP如何解决Deprecated: preg_replace()报错 Home / 开源程序 / ECSHOP / ECSHOP如何解决Deprecated: preg_replace()报错

    Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in D:\w ...

  8. EntityFramework追踪Sql语句

    方法一:SQL Profile 这个工具只有sql标准版(standard) 及以上版本才有,我装的是SqlServer2012 Express,所以采用方法2. 方法二:EntityFramewor ...

  9. Ubuntu系统下安装python2.7

    第一步:下载python2.7相关版本源码(例如:Python-2.7.4.tgz) 第二步:安装 1) 解压 $tar zxvf Python-2.7.4.tar.tgz 2)进入文件夹: $cd ...

  10. Silverlight中DataPager控件扩展

    大家一定遇到这样的情况,想改变一下SL的DataPager的显示信息,比如希望分页控件上显示数据的总数.那么就需要扩展一下DataPager控件即可. 其实扩展DataPager很简单,只要获取到Da ...