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

POJ 2553 The Bottom of Graph 强连通图题解

时间:2014-10-31 10:12:32      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   os   ar   for   

Description

We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E) is called a directed graph. 
Let n be a positive integer, and let p=(e1,...,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices(v1,...,vn+1). Then p is called a path from vertex v1 to vertex vn+1 in G and we say that vn+1 is reachable from v1, writing (v1→vn+1)
Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from vv is also reachable from w. The bottom of a graph is the subset of all nodes that are sinks, i.e., bottom(G)={v∈V|?w∈V:(v→w)?(w→v)}. You have to calculate the bottom of certain graphs.

Input

The input contains several test cases, each of which corresponds to a directed graph G. Each test case starts with an integer number v, denoting the number of vertices of G=(V,E), where the vertices will be identified by the integer numbers in the set V={1,...,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,...,ve,we with the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.

Output

For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.bubuko.com,布布扣

Sample Input

3 3
1 3 2 3 3 1
2 1
1 2
0

Sample Output

1 3
2

题意的本质是查找没有出度的强连通子图,没有出度就是sink,the bottom of graph了。

就是利用Tarjan算法求强连通子图,并要用标识号标识各个强连通子图,然后记录好各个顶点属于哪强连通子图。


程序带详细的注解:

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;

const int MAX_V = 5001;
vector<int> graAdj[MAX_V];//vector表示的邻接图
int conNo, vToCon[MAX_V];//强连通子图标号及顶点对应强连通子图号的数组
int low[MAX_V];//标识最低标识号,如果都属于这个标识号的顶点都属于同一连通子图
int stk[MAX_V], top;//数组表示栈
bool vis[MAX_V];//记录是否访问过的顶点
int out[MAX_V];//强连通子图的出度,如果出度为零,那么改强连通子图为sink

template<typename T>
inline bool equ(T t1, T t2) { return t1 == t2; }

void dfsTar(int u, int no = 1)
{
	low[u] = no;//每递归进一个顶点,初始表示low[]
	stk[++top] = u;//每个顶点记录入栈
	vis[u] = true;//标志好是否访问过了

	int n = (int)graAdj[u].size();
	for (int i = 0; i < n; i++)
	{
		int v = graAdj[u][i];
		if (!vis[v])
		{
			dfsTar(v, no+1);//这里递归
			if (low[u] > low[v]) low[u] = low[v];//更新最低标识号
		}
		else if (!vToCon[v] && low[u] > low[v]) low[u] = low[v];//更新
	}
	if (equ(low[u], no))//最低标识号和递归进的初始号相同就找到一个子图了
	{
		++conNo;
		int v;
		do
		{
			v = stk[top--];//出栈
			vToCon[v] = conNo;//顶点对应到子图号
		} while (v != u);//出栈到本顶点,那么改子图所有顶点出栈完毕
	}
}

void Tarjan(int n)
{
	conNo = 0;//记得前期的清零工作
	fill(vToCon, vToCon+n+1, 0);
	fill(low, low+n+1, 0);
	fill(vis, vis+n+1, false);
	top = -1;

	for (int u = 1; u <= n; u++) if (!vis[u]) dfsTar(u);
}

int main()
{
	int V, E, u, v;
	while(~scanf("%d %d", &V, &E) && V)
	{
		for (int i = 1; i <= V; i++)
		{
			graAdj[i].clear();//清零
		}
		for (int i = 0; i < E; i++)
		{
			scanf("%d %d", &u, &v);
			graAdj[u].push_back(v);//建立vector表示的邻接表
		}
		Tarjan(V);
		fill(out, out+conNo+1, 0);
		for (int u = 1; u <= V; u++)
		{
			int n = graAdj[u].size();
			for (int i = 0; i < n; i++)
			{
				int v = graAdj[u][i];
				if (vToCon[u] != vToCon[v])
				{
					out[vToCon[u]]++;//记录强连通子图号的出度数
				}
			}
		}
		for (int u = 1; u <= V; u++)//出度为零,即为答案:Graph Bottom
		{
			if (!out[vToCon[u]]) printf("%d ", u);
		}
		putchar('\n');
	}
	return 0;
}



POJ 2553 The Bottom of Graph 强连通图题解

标签:des   style   blog   http   io   color   os   ar   for   

原文地址:http://blog.csdn.net/kenden23/article/details/40649121

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