poj 2367 Genealogical tree 【基础拓扑排序】
2015-08-14 14:30
330 查看
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
Sample Output
2 4 5 3 1
题意:给你N个点,接下来有N行,第i行给出的是i节点的孩子节点输入以0结束。让你输出任意一种拓扑序列。
水题,直接正向拓扑即可。
AC代码:
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3643 | Accepted: 2425 | Special Judge |
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
题意:给你N个点,接下来有N行,第i行给出的是i节点的孩子节点输入以0结束。让你输出任意一种拓扑序列。
水题,直接正向拓扑即可。
AC代码:
#include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm> #define MAXN 100+10 using namespace std; vector<int> G[MAXN]; int in[MAXN]; int N; void init() { for(int i = 1; i <= N; i++) G[i].clear(), in[i] = 0; } void getMap() { int x, y; for(int x = 1; x <= N; x++) { while(scanf("%d", &y), y) G[x].push_back(y), in[y]++; } } void solve() { queue<int> Q; for(int i = 1; i <= N; i++) if(in[i] == 0) Q.push(i); int t = 0; while(!Q.empty()) { int u = Q.front(); Q.pop(); if(t > 0) printf(" "); printf("%d", u); t++; for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if(--in[v] == 0) Q.push(v); } } } int main() { while(scanf("%d", &N) != EOF) { init(); getMap(); solve(); } return 0; }
相关文章推荐
- JFinal 源码超详细解析之DB+ActiveRecord
- [自我记录贴] IntelliJ & Android Studio Issues
- C与C++编码规范
- 二维数组 我被你搞糊涂了
- mysql -- 按时间查询 今天、昨天、明天、上月....
- mybatis下的分页,支持所有的数据库
- ava I/O 操作及优化建议 NIO AIO
- 一道有关数据类型的笔试题
- AJAX--前后台交互
- PHP中session和cookie的区别
- 导出gmail邮件
- 返回上两个视图
- IOStime处理
- 不使用数据库,用程序快速排重
- HDU 5379 树形DP Mahjong tree
- 今天,我写下了我的第一篇博文。
- 教你如何使用WatchKit创建可选列表App
- php的数据类型和变量的作用域
- hdoj 4857 逃生 【拓扑排序 输出字典序最小解】
- 用IDM下载SAP软件,享受风一样的极速下载!