码迷,mamicode.com
首页 > 其他好文 > 详细

poj2367Genealogical tre

时间:2015-08-02 13:46:13      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:poj   拓扑排序   

原题连接:http://poj.org/problem?id=2367

思路:拓扑排序

#include<stdio.h>
#include<stdlib.h>
typedef struct vert
{
	int num;           //结点编号
	struct vert* next;
}G_v;
typedef struct node
{
	int InDgree,OutDgree;            //出度和入度
	G_v* next;
}G_n;
G_n P[101];                         //结点
int main(void)
{
	G_v *tmp;
	G_v *q;
	int n,i,a,j,count;
	scanf("%d",&n);
	for(i = 0;i <= n;i ++)
	{
		P[i].next = NULL;
		P[i].InDgree = P[i].OutDgree = 0;
	}
	i = 1;
	while(i <= n)
	{
		tmp = P[i].next;
		while(scanf("%d",&a)&&a != 0)
		{
			P[i].OutDgree ++;
			P[a].InDgree ++;
			q = (G_v*)malloc(sizeof(G_v));
			q->num = a;
			q->next = NULL;
			if(tmp == NULL)
			{
				P[i].next = q;
				tmp = P[i].next;
			}
			else
			{
				tmp->next = q;
				tmp = tmp->next;
			}
		}
		i ++;
	}
	count = 1;
	for(i = 1;i <= n;i ++)
	{
		if(P[i].InDgree == 0)
		{
			if(count++ == n)
			{
			   printf("%d\n",i);
			   break;
			}
			else
				printf("%d ",i);
			tmp = P[i].next;
			while(tmp)
			{
				P[tmp->num].InDgree --;
				tmp = tmp->next;
			}
		    P[i].InDgree = 200;
			i = 0;
		}
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

poj2367Genealogical tre

标签:poj   拓扑排序   

原文地址:http://blog.csdn.net/ibigprogramer/article/details/47205923

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!