POJ 2367 Genealogical tree(拓扑排序DFS解法)

题目来源:http://poj.org/problem?id=2367

问题描述

Genealogical tree

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 7878

 

Accepted: 5122

 

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


Source

Ural State University Internal Contest October'2000 Junior Session

------------------------------------------------------------

题意

拓扑排序模板题。给定有向图的邻接表,保证拓扑排序存在,求拓扑排序(若不唯一输出任意一个即可)

------------------------------------------------------------

思路

从每个节点出发,dfs遍历整张图上没有访问过的节点,当不能遍历时,将该节点入栈。

因为拓扑顺序最后的节点最先入栈,拓扑顺序最前的节点最后入栈,故从栈顶开始按顺序出栈即可得到拓扑顺序。

拓扑排序的dfs解法的缺点是不能直接判断环。

------------------------------------------------------------

代码

// 拓扑排序:DFS方法

#include<cstdio>
#include<vector>

const int NMAX = 30;
std::vector<int> E[NMAX];		// 邻接表

void dfs(int i, bool *vis, std::vector<int> & res)
	// i:当前访问节点, vis:是否访问数组, res:拓扑排序结果
{
	vis[i] = 1;
	int j;
	for (j=0; j<E[i].size(); j++)
	{
		if (!vis[E[i].at(j)])
		{
			dfs(E[i].at(j), vis, res);
		}
	}
	res.push_back(i);				// 直到找到没有出度的节点放入res
}

void toposort(int n)					// n: 总节点数
{
	bool * vis = new bool[n]();
	std::vector<int> res;
	int i;
	for (i=0; i<n; i++)
	{
		if (!vis[i])
		{
			dfs(i, vis, res);
		}
	}
	delete[] vis;
	while (!res.empty())
	{
		i = res.back();
		res.pop_back();
		printf("%d ", i+1);		// 将[0,n)映射到输出要求的(0,n]
	}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("2367.txt", "r", stdin);
#endif
	int n, i, v;
	scanf("%d", &n);
	for (i=0; i<n; i++)
	{
		while (scanf("%d", &v))
		{
			if (v == 0)
			{
				break;
			}
			E[i].push_back(--v);
		}
	}
	toposort(n);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/82530941