POJ 2367 非常基本的拓扑排序题 用GCC可以AC

问题描述

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.

输入

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.

(根据红字部分,可知行星依次从1到N来命名,其子行星树,可能为0)

输出

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.

样例输入

5

0

4 5 1 0

1 0

5 3 0

3 0

样例输出

2 4 5 3 1

问题来源

Ural State University Internal Contest October‘2000 Junior Session

完整C代码

#include <stdio.h>
#include <stdlib.h>

typedef struct gNode {
	int vertex;
	int in;
	struct gNode *next;
}gNode;

void CreateAOV( gNode **g, int n )
{
	gNode *p;
	gNode *q;
	int child;
	(*g) = (gNode *)malloc(sizeof(gNode)*n);
	int i;
	for( i=0; i<n; i++ )
		(*g)[i].in = 0;
	for( i=0; i<n; i++ ) {
		(*g)[i].vertex = i+1;			//根据题意依次将顶点赋值为1,2,...,n
		(*g)[i].next = NULL;
		q = *g + i;
		scanf("%d", &child);
		while( 0 != child) {
			p = (gNode *)malloc(sizeof(gNode));
			p->vertex = child;
			p->next = NULL;
			q->next = p;
			q = p;
			(*g)[child-1].in ++;		//由child数值,对相应编号为child-1的顶点入度进行加1
			scanf("%d", &child);
		}
	}
}

void TopSort( gNode *g, int n )
{
	int i;
	int j;
	int k;
	gNode *p;

	for( i=0; i<n; i++ )
		for( j=0; j<n; j++ )
			if( 0 == g[j].in ) {
				printf("%d ", g[j].vertex);
				g[j].in = -1;
				p = g[j].next ;
				while( NULL != p ) {
					for( k=0; k<n; k++ )
						if( p->vertex == g[k].vertex ) {
							g[k].in--;
							break;
						}
					p = p->next ;
				}
				break;
			}
			printf("\n");
}

int main()
{
	int n;
	gNode *g;
	scanf("%d", &n);

	CreateAOV( &g, n );
	TopSort( g, n );
	return 0;
}
时间: 2024-12-18 11:54:28

POJ 2367 非常基本的拓扑排序题 用GCC可以AC的相关文章

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 Genealogical tree (拓扑排序基础题)

题目链接:http://poj.org/problem?id=2367 题目: 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 paren

poj 2367 Genealogical tree【拓扑排序输出可行解】

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

POJ 2367 Genealogical tree(拓扑排序)

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

POJ 1128 Frame Stacking(拓扑排序&amp;#183;打印字典序)

题意  给你一些矩形框堆叠后的鸟瞰图  推断这些矩形框的堆叠顺序  每一个矩形框满足每边都至少有一个点可见  输入保证至少有一个解 按字典序输出全部可行解 和上一题有点像  仅仅是这个要打印全部的可行方案  建图还是类似  由于每一个矩形框的四边都有点可见  所以每一个矩形框的左上角和右下角的坐标是能够确定的  然后一个矩形框上有其他字符时  就让这个矩形框相应的字符和那个其他字符建立一个小于关系  由于要打印方案  所以在有多个入度为0的点时须要用DFS对每种选择都进行一遍拓扑排序 #incl

POJ 3249 Test for Job 拓扑排序+DP

http://poj.org/problem?id=3249 题意: 给一个有向无环图DAG(不一定联通),每个点有权值,入度为0的点为起点,出度为0的点为终点,选择一个起点走到一个终点,使得路上的权和最大. 分析: dp[to] = max(dp[from]) + value[to],然后先拓扑排序保证状态正确转移即可,终点做标记,如果是终点则尝试更新答案. update:因为点权可以为负,所以程序里用dp[i] == -1表示未访问过该点是有问题的,不过没有遇上会卡掉这种情况的数据=.= 1

POJ 3687 Labeling Balls【拓扑排序 优先队列】

题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.csdn.net/scf0920/article/details/28108243 然后用优先队列来实现的参看的这一篇 http://ycool.com/post/u9ahrwg#algo3 1 #include<iostream> 2 #include<cstdio> 3 #includ

POJ 3648 Wedding(2-SAT 拓扑排序输出任意一种解决方案)

题目链接:http://poj.org/problem?id=3648 Description Up to thirty couples will attend a wedding feast, at which they will be seated on either side of a long table. The bride and groom sit at one end, opposite each other, and the bride wears an elaborate h