【POJ2367】Genealogical tree

布满荆棘的人生 2022-07-18 19:56 224阅读 0赞

Genealogical tree

Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu

Submit Status

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

  1. 5
  2. 0
  3. 4 5 1 0
  4. 1 0
  5. 5 3 0
  6. 3 0

Sample Output

  1. 2 4 5 3 1

题目大意:第一行有整数n,接下来n行,第n行表示n的孩子(可以没有)的编号,必须按照由长辈到晚辈的顺序输出。

拓扑代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. const int N = 105;
  7. int map[N][N],degree[N],que[N];
  8. int n;
  9. void topo() {
  10. int t=0,m;
  11. for(int l=1; l<=n; l++) {
  12. for(int i=1; i<=n; i++) {
  13. if(!degree[i]) {
  14. m=i;
  15. break;
  16. }
  17. }
  18. que[t++]=m,degree[m]=-1;
  19. for(int j=1; j<=n; j++) {
  20. if(map[m][j]) {
  21. degree[j]--;
  22. }
  23. }
  24. }
  25. printf("%d",que[0]);
  26. for(int l=1; l<n; l++)
  27. printf(" %d",que[l]);
  28. printf("\n");
  29. }
  30. int main() {
  31. while(scanf("%d",&n)!=EOF) {
  32. memset(degree,0,sizeof(degree));
  33. memset(map,0,sizeof(map));
  34. for(int l=1; l<=n; l++) {
  35. int m;
  36. scanf("%d",&m);
  37. while(m) {
  38. if(!map[l][m]) {
  39. map[l][m]=1;
  40. degree[m]++;
  41. scanf("%d",&m);
  42. }
  43. }
  44. }
  45. topo();
  46. }
  47. return 0;
  48. }

发表评论

表情:
评论列表 (有 0 条评论,224人围观)

还没有评论,来说两句吧...

相关阅读

    相关 POJ--2255 Tree recovery

    补一下这一道恢复树的题目,前面好就做的吧。 题意:   就是给你一个前序遍历树和一个中序遍历树,让你恢复后序遍历树。([树的遍历][Link 1]) 解法: 利用了前序

    相关 poj2367 拓扑排序入门

    先来一道拓扑排序的裸题吧!! 首先要知道拓扑排序的概念,拓扑排序就是,先找到入度为0的点,删去,同时把它的所有出度删去,再找新的入度为0的点,删去的点的顺序就是拓扑序

    相关 [poj1741]Tree

    点分治模板题,可以将同一棵树的链分为两种:1.通过重心;2.在子树内部。第2种可以搜下去,第1种的答案即$\\sum\_\{i,j\}\[di+dj<=m\]-\\sum\\l

    相关 poj2054 Color a Tree

    神题。这题是巨毒瘤... 自己写真可谓是: 排空驭气奔如电,上天入地求之遍 上穷碧落下黄泉,两处茫茫皆不见 由于我们知道:不是树形时,不停选值最大的节点可以得到最小代价