poj 2367 Genealogical tree(裸拓扑)
来源:互联网 发布:mac如何删除应用程序 编辑:程序博客网 时间:2024/05/05 01:40
Genealogical tree
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4837 Accepted: 3213 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.
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
Source
Ural State University Internal Contest October'2000 Junior Session
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int maxn = 10005;int n, k, head[maxn], degree[maxn];struct node{int v, next;}edge[maxn];void addEdge(int u, int v){edge[k].v = v;edge[k].next = head[u];head[u] = k++;}void topSort(void){queue<int> q;int count = 0, flag = 0;for(int i = 1; i <= n; i++)if(degree[i] == 0)q.push(i), count++;while(!q.empty()){int u = q.front(); q.pop();if(flag++) printf(" ");printf("%d", u);for(int t = head[u]; t != -1; t = edge[t].next){int to = edge[t].v;if(--degree[to] == 0) q.push(to);}}puts("");}int main(void){while(cin >> n){k = 0;memset(degree, 0, sizeof(degree));memset(head, -1, sizeof(head));for(int i = 1; i <= n; i++){int t;while(scanf("%d", &t), t)addEdge(i, t), degree[t]++;}topSort();}return 0;}
0 0
- 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 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 (拓扑排序)
- Genealogical tree POJ 2367【拓扑排序】
- ios NSDictionary
- 基于Web3D的VR
- 排序算法-选择排序
- 172.Remove Element-删除元素(容易题)
- 初学AngularJs 神奇的框架
- poj 2367 Genealogical tree(裸拓扑)
- Maven 入门教程(三)
- Win7下Eclipse+MinGW+msys编译ffmpeg过程
- js创建对象的几种常用方式小结
- Tyvj P1863 [Poetize I]黑魔法师之门
- DrawerLayout的使用
- 最新ffmpeg编译和用eclipse进行源码调试(linux)
- Koa框架实践与中间件原理剖析
- Java基础-面向对象小知识(下)