zoukankan      html  css  js  c++  java
  • Genealogical tree

    Genealogical tree

    Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6032 Accepted: 3973 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

    5

    0

    4 5 1 0

    1 0

    5 3 0

    3 0

    Sample Output

    2 4 5 3 1

    Source

    Ural State University Internal Contest October'2000 Junior Session

     

    //题意: 求出任意一个 1 -- n 的拓扑排列,第一行是 n ,然后 i 行,每行一些数 x ,代表 i 要求在 x 前,0代表结束该行输入

    //拓扑排序模板题

    DFS

     1 # include <cstring>
     2 # include <cstdio>
     3 # include <cstdlib>
     4 # include <iostream>
     5 # include <vector>
     6 # include <queue>
     7 # include <stack>
     8 # include <map>
     9 # include <bitset>
    10 # include <sstream>
    11 # include <set>
    12 # include <cmath>
    13 # include <algorithm>
    14 # pragma  comment(linker,"/STACK:102400000,102400000")
    15 using namespace std;
    16 # define LL          long long
    17 # define pr          pair
    18 # define mkp         make_pair
    19 # define lowbit(x)   ((x)&(-x))
    20 # define PI          acos(-1.0)
    21 # define INF         0x3f3f3f3f3f3f3f3f
    22 # define eps         1e-8
    23 # define MOD         1000000007
    24 
    25 inline int scan() {
    26     int x=0,f=1; char ch=getchar();
    27     while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
    28     while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    29     return x*f;
    30 }
    31 inline void Out(int a) {
    32     if(a<0) {putchar('-'); a=-a;}
    33     if(a>=10) Out(a/10);
    34     putchar(a%10+'0');
    35 }
    36 const int N = 105;
    37 /**************************/
    38 
    39 int n;
    40 bool G[N][N];
    41 int ans[N];
    42 int vis[N];
    43 int dex;
    44 
    45 bool dfs(int u)
    46 {
    47     vis[u]=-1;                   //标记为正在遍历的
    48     for (int v=1;v<=n;v++)
    49     {
    50         if (G[u][v])
    51         {
    52             if (vis[v]==-1) return 0;       //说明组成了环,不能拓扑排序
    53             else if (!vis[v]&&!dfs(v)) return 0; //继续遍历未遍历的
    54         }
    55     }
    56     vis[u]=1;       //标记遍历过了
    57     ans[dex--]=u;    //输出的就是这个数组
    58     return 1;
    59 }
    60 
    61 bool toposort()
    62 {
    63     dex=n;
    64     for (int i=1;i<=n;i++)               //将所有数都遍历
    65         if (!vis[i]&&!dfs(i)) return 0;
    66     return 1;
    67 }
    68 int main()
    69 {
    70     while (scanf("%d",&n)!=EOF)
    71     {
    72         memset(G,0,sizeof (G));
    73         memset(vis,0,sizeof(vis));
    74         for (int i=1;i<=n;i++)
    75         {
    76             int x;
    77             while (1)
    78             {
    79                 x = scan();
    80                 if (x==0) break;
    81                 G[i][x]=1;
    82             }
    83         }
    84         if (toposort())
    85         {
    86              for (int i=1;i<n;i++)
    87                 printf("%d ",ans[i]);
    88              printf("%d
    ",ans[n]);
    89         }
    90     }
    91     return 0;
    92 }
    View Code

     

  • 相关阅读:
    熟悉常用的Linux操作
    Python基础综合练习
    简易c语言文法
    词法分析程序
    组合数据类型综合练习
    综合练习:词频统计
    词法分析程序2
    我对编译原理的理解
    【分享】博客美化(6)为你的博文自动添加目录
    python爬虫的基本思路
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/7420002.html
Copyright © 2011-2022 走看看