POJ NO.2367 Genealogical tree(简单拓扑排序,典例)
来源:互联网 发布:win7如何运行python 编辑:程序博客网 时间:2024/05/29 02:52
问题描述:
给出顺序关系让你排除先后,即简单地拓扑排序。
图的存储用链式前向星。
原题描述:
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.
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
504 5 1 01 05 3 03 0
Sample Output
2 4 5 3 1
代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<stack>#include<cstring>#include<string>#include<vector>#include<set>#define X first#define Y secondusing namespace std;const int INF = 0x3f3f3f3f;const int MAX = 110;int head[MAX], que[MAX], indegree[MAX];int ans, top, n;struct node{ int to, next;}edge[MAX<<1];void init(){ memset(head, -1, sizeof(head)); memset(indegree, 0, sizeof(indegree));}void add_edge(int u, int v){ edge[top].to = v; edge[top].next = head[u]; head[u] = top++;}void Topsort(){ 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 j = head[que[i]]; j != -1; j = edge[j].next){ indegree[edge[j].to]--; if(indegree[edge[j].to] == 0) que[iq++] = edge[j].to; } } for(int i = 0; i < iq; i++) printf("%d ", que[i]); printf("\n");}int main(){ while(~scanf("%d", &n)){ init(); for(int i = 1; i <= n; i++){ int t; while(scanf("%d", &t) && t){ add_edge(i, t); indegree[t]++; } } Topsort(); } return 0;}
0 0
- POJ NO.2367 Genealogical tree(简单拓扑排序,典例)
- POJ 2367Genealogical tree 简单拓扑排序
- POJ 2367 Genealogical tree(简单拓扑排序)
- POJ:2367 Genealogical tree(普通简单版拓扑排序)
- POJ 2367:Genealogical tree(拓扑排序)
- POJ 2367 Genealogical tree(拓扑排序)
- POJ 2367 Genealogical tree(拓扑排序)
- POJ 2367 Genealogical tree (拓扑排序)
- POJ 2367 Genealogical tree (拓扑排序)
- POJ-2367 Genealogical tree(拓扑排序)
- POJ 2367Genealogical tree(拓扑排序)
- POJ 2367:Genealogical tree (拓扑排序)
- poj 2367 Genealogical tree (拓扑排序)
- POJ 2367 Genealogical tree 拓扑排序
- POJ 2367 Genealogical tree [拓扑排序]
- Poj 2367 Genealogical tree(拓扑排序)
- 【POJ】2367 Genealogical tree 拓扑排序
- POJ 2367 Genealogical tree 拓扑排序入门
- IPC机制———读书笔记
- test
- mysql中的当前时间函数
- 【学习C++】C++ Primer Plus (第六版)第七章编程练习6-10
- B 树、B- 树、B+ 树、B* 树都是什么
- POJ NO.2367 Genealogical tree(简单拓扑排序,典例)
- QT中QProcess调用命令行的痛苦经历
- Cache
- 10种linux下磁盘快照方式恢复系统
- acm 兔子繁殖
- eth0,eth1,eth2,lo是什么
- [树上倍增] BZOJ 4281 [ONTAK2015]Związek Harcerstwa Bajtockiego
- JPush极光推送问题,用户是否打开允许推送信息的开关,但不管设置没设置都会弹出警告说没有设置
- 设计模式-13-代理模式