POJ 2367 Genealogical tree(简单拓扑排序)
2015-08-20 09:27
561 查看
题意:给定一些辈分关系,按辈分给他们排序
解题思路:沙茶拓扑排序,不用判断是否有环,不用反向建图,不用判断拓扑序是否唯一,不用输出含有指定要求的拓扑序列,直接最最普通的拓扑排序,裸过。
数据结构选用链式向前星。
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
解题思路:沙茶拓扑排序,不用判断是否有环,不用反向建图,不用判断拓扑序是否唯一,不用输出含有指定要求的拓扑序列,直接最最普通的拓扑排序,裸过。
数据结构选用链式向前星。
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<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<list> #include<iostream> #include<map> #include<queue> #include<set> #include<stack> #include<vector> using namespace std; const int INF = 0x3f3f3f3f; int buf[10]; //整型变量快速输入输出函数 inline int readint() { char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0; while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } return x; } inline void writeint(int i) { int p = 0; if(i == 0) p++; else while(i) { buf[p++] = i % 10; i /= 10; } for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]); } //////////////////////////////////////////////////////////////////////// #define MAX_N 105 int indegree[MAX_N]; int n; int top = 0; int head[MAX_N]; struct node { int to, next; }; node edge[MAX_N << 1]; int add_edge(int u, int v) { edge[top].to = v; edge[top].next = head[u]; head[u] = top++; } void top_sort() { int que[MAX_N]; int iq = 0; for(int i =1 ; i <= n ; i++) { if(indegree[i] == 0) { que[iq++] = i; } } for(int i = 0 ; i < iq ; i++) { for(int k = head[que[i]]; k != -1 ; k = edge[k].next) { indegree[edge[k].to]--; if(indegree[edge[k].to] == 0) { que[iq++] = edge[k].to; } } } for(int i = 0 ; i < iq ; i++) cout<<que[i]<<" "; cout<<endl; } int main() { memset(head, -1, sizeof(head)); memset(indegree, 0, sizeof(indegree)); top = 0; n = readint(); for(int i = 1; i <= n ; i++) { int num; while(scanf("%d", &num)&& num) { add_edge(i, num); indegree[num]++; } } top_sort(); return 0; }
相关文章推荐
- Csharp: read excel file using Open XML SDK 2.5
- Empower Developers
- Architects’ Focus Is on the Boundaries and Interfaces
- ABAP/4 SQL 中for all entries in 理解
- iOS 对时间排序
- Yii自定义验证规则
- php curl模拟post请求提交数据例子总结
- ubuntu15.04 xampp 安装memcache
- java 中的 wait()方法和 sleep()方法的区别是什么?
- 第一次写博客,学会分享,乐于奉献
- Janus the Architect
- Html做三个平台原生APP啦
- IOS照相
- ubuntu15.04 xampp 安装memcache
- CentOS系统下的Hadoop集群(第5期副刊)_JDK和SSH无密码配置
- Android Fragment使用注意事项
- 亚马逊领导力准则
- iOS lable内容格式的设置
- Algorithms—128.Longest Consecutive Sequence
- 如何在多台机器上共享IOS证书