Genealogical tree(拓扑排序)

你的名字 2022-08-04 13:38 177阅读 0赞

Genealogical tree
















Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3773   Accepted: 2508   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.

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

Source

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. const int N=105;
  5. using namespace std;
  6. int f,num[N];
  7. int mp[N][N],indegree[N];
  8. void toposort(int mp[N][N],int indegree[N],int n)
  9. {
  10. int i,j,k;
  11. for(i=1;i<=n;i++)//遍历N次
  12. {
  13. for(j=1;j<=n;j++)//找出入读为0的节点
  14. {
  15. if(indegree[j]==0)
  16. {
  17. indegree[j]--;
  18. num[f++]=j;
  19. for(k=1;k<=n;k++)
  20. {
  21. if(mp[j][k]==1)//有关系!
  22. {
  23. indegree[k]--;
  24. }
  25. }
  26. break;
  27. }
  28. }
  29. }
  30. for(i=0;i<f-1;i++)
  31. {
  32. cout<<num[i]<<" ";
  33. }
  34. cout<<num[f-1]<<endl;
  35. }
  36. int main()
  37. {
  38. int n,i,a;
  39. while(cin>>n)
  40. {
  41. f=0;
  42. memset(mp,0,sizeof(mp));
  43. memset(indegree,0,sizeof(indegree));
  44. memset(num,0,sizeof(num));
  45. for(i=1;i<=n;i++)
  46. {
  47. while(cin>>a,a)
  48. {
  49. if(mp[i][a]==0)
  50. {
  51. mp[i][a]=1;
  52. indegree[a]++;
  53. }
  54. }
  55. }
  56. toposort(mp,indegree,n);
  57. }
  58. }

发表评论

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

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

相关阅读

    相关 拓扑排序

    什么是拓扑排序? 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序

    相关 拓扑排序

    拓 扑 排 序 一个较大的工程往往被划分成许多子工程,我们把这些子工程称作活动(activity)。在整个工程中,有些子工程(活动)必须在其它有关子工程完成之后才能开始,也就

    相关 拓扑排序

    (1)有向无环图 无环的有向图,简称 DAG (Directed Acycline Graph) 图。 有向无环图在工程计划和管理方面的应用:除最简单的情况之外,几

    相关 拓扑排序

    拓扑排序: 拓扑排序是根据离散数学中有关偏序与全序定义的。 ![0_1314168765l7fq.gif][] 若一个集合 X 上的关系 R 是自反的 反

    相关 拓扑排序

     一个较大的工程往往被划分成许多子工程,我们把这些子工程称作活动(activity)。在整个工程中,有些子工程(活动)必须在其它有关子工程完成之后才能开始,也就是说,一个子工程