首页 > 代码库 > POJ 2367 Genealogical tree 拓扑排序入门

POJ 2367 Genealogical tree 拓扑排序入门

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题意:  输入n表示有n个人,然后有n行输入,分别表示1~n号的上级(可多个,到0结束) 然后输出它们的拓扑排序入门题 上了很多的条件 裸题 提供两种模板算法。。深度优先:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int g[105][105];
int n,ad;
bool vis[105];
int ans[105];
void dfs(int i)               // dfs到没有后继元素 则最大的是它
{
    vis[i]=true;
    for(int j=1; j<=n; j++)
        if(!vis[j]&&g[i][j])
            dfs(j);
            
    ans[ad--]=i;         //深度优先是将ans从后往前推得  

}
void toposort()
{
    ad=n;                      
    memset(vis,false,sizeof(vis));
    for(int i=1; i<=n; i++)
    {
        if(!vis[i])             //十分重要
        {
            dfs(i);
        }
    }
}
int main()
{
    int x;
    while(cin>>n)
    {
        memset(g,0,sizeof(g));
        for(int i=1; i<=n; i++)
        {
            while(1)
            {
                scanf("%d",&x);
                if(x==0)
                    break;
                g[i][x]=1;        
            }
        }
        toposort();
        int i;
        for(i=1; i<n; i++)
            cout<<ans[i]<<' ';
        cout<<ans[i];
    }
    return 0;
}
广度优先:
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
#define qq 105
int map[qq][qq];
int pre[qq];
int n;
void bfs()
{
    int i,j;
    int x=0;
    queue<int>q;
    int now,next;
    for(i=1; i<=n; i++)
        if(pre[i]==0)
            q.push(i);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(x==1)
            cout<<' ';
        x=1;
        cout<<now;
        j=1;
        while(map[now][j]!=0)
        {
            pre[map[now][j]]--;
            if(pre[map[now][j]]==0)
                q.push(map[now][j]);
            j++;
        }
    }
    cout<<endl;
    return;
}
int main()
{
    int i,j;
    while(cin>>n)
    {
        for(i=1; i<=n; i++)
            pre[i]=0;
        for(i=1; i<=n; i++)
        {
            int a;
            j=1;
            while(cin>>a&&a)
            {
                map[i][j++]=a;
                pre[a]++;
            }
        }
        bfs();
    }
    return 0;
}

广度优先用的是最原始的拓扑排序算法思想 充分利用队列的优势。。