POJ 2367 Genealogical tree

拓扑排序 : 对一个 有向无环图 (Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序列中出现在v之前。通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列。简单的说,由某个集合上的一个 偏序 得到该集合上的一个 全序 ,这个操作称之为拓扑排序。
http://blog.csdn.net/fisher_jiang/article/details/941234
http://www.cnblogs.com/newpanderking/archive/2012/10/18/2729552.html点击打开链接

Genealogical tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5415 Accepted: 3546 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
 
 
题意  :给你一些顶点,先是输入总顶点数,第几行就代表以该数字为起点,这一行里面的值则代表该行的起点会指向哪些点,当顶点为0时
,则表示该行的输入结束,输入另一行的元素,然后输出拓扑排序。   拓扑排序是指 在一个有向图中,找出入度为0的那个顶点,然后输出
该顶点,去掉该顶点指向其他顶点的边,然后对应的顶点的入度要减1.  然后重新在图中找入度为0的顶点,然后输出该顶点,去掉该顶点指向
其他顶点的边,然后对应的顶点的入度要减1。一直重复,直到所有顶点输出
代码如下:
#include <ctime>   
using namespace std;  
int n, m;  
int in[201];  
int ans[201];  
vector<int>v[201];  //定义一个动态数组
void topsort()  
{  
    int i, count = 0;  
    queue<int>q;    //定义一个队列
    for(i = 1; i <= n; i++)  
    {  
        if(in[i] == 0)  //判断当前顶底是否已经存在于输入数组中
        q.push(i);  //如果没就把它入栈
    }  
    while(!q.empty())  
    {  
        int first = q.front();  //如果队列没有空就把队首元素记录下来然后输出
        ans[count++] = first;  //把队首元素的值存入输出数组中,等下用来输出
        q.pop();  
        int size = v[first].size();  
        for(i = 0; i < size; i++)  
        {  
            int can_reach = v[first][i]; //得到某个顶点 
            in[can_reach]--;  
            if(in[can_reach] == 0) //判断某个顶点去除目前这个顶点指向它的路径之后是否如入度为0 
            q.push(can_reach);  //如果为0就入栈
        }  
    }  
}  
int main()  
{  
    int i, tmp;  
    scanf("%d", &n);  
    for(i = 1; i <= n; i++)  
    {  
        while(1)  
        {  
            scanf("%d", &tmp);  
            if(tmp == 0)  //如果该元素等于0,表示该顶点指向的顶点已经全部列举完了
            break;  
            v[i].push_back(tmp);  //把该元素输入到对应的起点的空间
            in[tmp]++;  //对本节的的入度进行++,因为又有一个顶点指向该顶点了
        }  
    }  
    topsort();  
    for(i = 0; i < n - 1; i++)  
    printf("%d ", ans[i]);  
    printf("%d\n", ans[n - 1]);  
    return 0;  
}  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值