POJ 2367Genealogical tree 简单拓扑排序

Genealogical tree

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3358   Accepted: 2255   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

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#define N 1009
using namespace std;

int mp[N][N];
int ans[N];
int deg[N];

int main()
{
    int n;
    int a;

    while(~scanf("%d",&n))
    {
        memset(mp,0,sizeof mp);

        for(int i=1;i<=n;i++)
        {
            while(1)
            {
                scanf("%d",&a);
                if(a==0) break;
                mp[a][i]=1;
                deg[a]++;
            }
        }

        for(int i=0;i<n;i++)
        {
            for(int j=1;j<=n;j++)
                if(deg[j]==0)
                {
                    ans[i]=j;
                    deg[j]=-1;
                    for(int k=1;k<=n;k++)
                    {
                        if(mp[k][j])
                        deg[k]--;
                    }

                    break;
                }
        }
        cout<<ans[0];
        for(int i=1;i<n;i++)
        cout<<" "<<ans[i];

        cout<<endl;
    }

    return 0;
}
时间: 2024-10-12 10:02:29

POJ 2367Genealogical tree 简单拓扑排序的相关文章

POJ 2367:Genealogical tree(拓扑排序)

Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2738 Accepted: 1838 Special Judge Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They ga

poj 1270 Following Orders(拓扑排序+dfs)

大致题意:每个样例包含两行,第一行输入n个字符,可能是无序的.第二行输入成对的a b,代表a要在b前面.输出所有的符合这样的序列. 思路:很明显的拓扑排序.要输出所有的序列,那么就从入度为0的点进行dfs,每次选择一个入度为0的点,加入输出序列并把与它相邻的点的入度减一.dfs结束后要把状态再改回来. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include

[ACM] POJ 3687 Labeling Balls (拓扑排序,逆向建边)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

UVA 124 &amp; POJ 1270 Following Orders(拓扑排序)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=60 http://poj.org/problem?id=1270 Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3806   Accepted: 1507 Description Or

poj 2367 Genealogical tree (拓扑排序)

火星人的血缘关系很奇怪,一个人可以有很多父亲,当然一个人也可以有很多孩子.有些时候分不清辈分会产生一些尴尬.所以写个程序来让n个人排序,长辈排在晚辈前面. 输入:N 代表n个人 1~n 接下来n行 第i行表示第i个人的孩纸,无序排列,可能为空.0代表一行输入结束. (大概我的智商真的不合适,否则怎么这么久了连个拓扑排序都写不好,T了三次..) 代码: /******************************************** Problem: 2367 User: Memory:

POJ 2367 Genealogical tree【拓扑排序】

题意:大概意思是--有一个家族聚集在一起,现在由家族里面的人讲话,辈分高的人先讲话.现在给出n,然后再给出n行数 第i行输入的数表示的意思是第i行的子孙是哪些数,然后这些数排在i的后面. 比如样例 5 0 4 5 1 0 1 0 5 3 0 3 0 1后面没有数 2后面有4 5 1 3后面有1 4后面有5 3 5后面有3 拓扑排序的一点小体会 (1)先把图储存下来,然后储存相应顶点的度数 (2)在图中选择没有前驱的点(即入度为0的点),加入队列中 (3)将以这一点为起点的边删去(将这一点指向的点

POJ 2367 (裸拓扑排序)

http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我也是刚刚才接触,想法还是挺简单的.实现起来也不复杂. 1 #include <stdio.h> 2 #include <string.h> 3 4 int Indegree[101],n; 5 6 bool mp[101][101]; 7 8 int topsort() 9 { 10

[ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 toN in such a way that: No two balls share

Codeforces Round #363 Fix a Tree(树 拓扑排序)

先做拓扑排序,再bfs处理 #include<cstdio>#include<iostream>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#include<map>#include<stack>#include<queue>#include<vector>#include<cmath&g