POJ 2367 Genealogical tree(拓扑排序+dfs)

21 篇文章 0 订阅
3 篇文章 0 订阅

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<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
using namespace std;
int vis[105];//存该点有没有被输出过
int a[105][105];//存每一个点的儿子
int num[105],n,tag;//num计算每一个点的儿子数目
void dfs(int ans)//ans记入输出了几个点
{
    if(ans>=n||tag)//只要找到一个就全停
    {
        tag=1;
        return;
    }
    int i,j;
    int p[105];
    memset(p,0,sizeof(p));//初始化为0,如果还没输出的点中出现了这个点是他的儿子则这个点不能用
    for(i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            for(j=0;j<num[i];j++)//对每一个点的儿子进行遍历覆盖
            {
                p[a[i][j]]=1;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        if(!p[i]&&!vis[i])//找到一个没有输出的点里面没人是他的爸爸就输出,hhhh这句话有点好笑
        {
            if(ans==0)
            {
                printf("%d",i);
            }
            else
                printf(" %d",i);
            vis[i]=1;//输出过了
            break;
        }
    }
    dfs(ans+1);//很好,下一个
}
int main()
{
    int x,i,j,tot,sum;
    while(scanf("%d",&n)!=EOF)
    {
        tag=0;
        memset(vis,0,sizeof(vis));
        memset(num,0,sizeof(num));
        for(i=1;i<=n;i++)
        {
            while(scanf("%d",&x)&&x)
            {
                a[i][num[i]]=x;
                num[i]++;
            }
        }
        dfs(0);
        printf("\n");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值