您的位置:首页 > 大数据 > 人工智能

【HDU】3394 Railway 点双连通求块

2014-07-15 20:11 302 查看

Railway

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1046 Accepted Submission(s): 398



Problem Description
There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If
a railway belongs to more than one tourist routes, there might be clash on it, and if a railway belongs to none tourist route, it doesn’t need to build.

Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.


Input
The Input consists of multiple test cases. The first line of each test case contains two integers, n (0 < n <= 10000), m (0 <= m <= 100000), which are the number of locations and the number of the railways.
The next m lines, each line contains two integers, u, v (0 <= u, v < n), which means the manger plans to build a railway on the road between u and v.

You can assume that there is no loop and no multiple edges.

The last test case is followed by two zeros on a single line, which means the end of the input.


Output
Output the number of railways that are no need to build, and the number of railways where clash might happen. Please follow the format as the sample.


Sample Input
8 10
0 1
1 2
2 3
3 0
3 4
4 5
5 6
6 7
7 4
5 7
0 0




Sample Output
1 5




Author
momodi@whu


Source
The 5th Guangting
Cup Central China Invitational Programming Contest

传送门:【HDU】3394 Railway

题目大意:一个公园里面有n个景点,建设了m条道路,现在该建设者想知道如果为每个环形的路线选择旅游团,是否可能有的路会发生旅游团之间的争吵。争吵的定义是:两个旅游团出现在了同一条路径上,即存在两个环形路线部分道路重叠。并且他还想知道有哪些道路是不需要建设的,一条道路不在任何环形路线上的称之为不需要建设的路线。你的任务是输出不需要建设的道路的数量以及旅游团可能会发生争吵的道路的数量。

题目分析:不需要建设的道路就是桥。一个环形路线其实就是点连通分量。可能会发生争吵的道路我们可以这样分析:如果一个x个点的点连通分量里面包括x条边,则该连通分量中恰好包括一个环,如果有超过x条边,则说明至少有三个环(随便加一条边就有三个环),则所有的边都是会发生争吵的边。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REPV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define clear( a , x ) memset ( a , x , sizeof a )

const int MAXN = 10005 ;
const int MAXE = 100005 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
	int v , n ;
	Edge ( int var = 0 , int next = 0 ) :
		v ( var ) , n ( next ) {}
} ;

struct BCC {
	Edge edge[MAXE << 1] ;
	int adj[MAXN] , cntE ;
	int bcc[MAXN] ;
	int low[MAXN] , dfn[MAXN] , dfs_clock ;
	int S[MAXN] , top ;
	int clash_way , bridge ;
	bool vis[MAXN] ;
	
	void init () {
		top = cntE = dfs_clock = 0 ;
		clash_way = bridge = 0 ;
		clear ( dfn , 0 ) ;
		clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v ) {
		edge[cntE] = Edge ( v , adj[u] ) ;
		adj[u] = cntE ++ ;
		edge[cntE] = Edge ( u , adj[v] ) ;
		adj[v] = cntE ++ ;
	}
	
	void circuit () {
		int cnt = 0 ;
		clear ( vis , 0 ) ;
		REPF ( i , 1 , bcc[0] )
			vis[bcc[i]] = 1 ;
		REPF ( _ , 1 , bcc[0] ) {
			int u = bcc[_] ;
			for ( int i = adj[u] ; ~i ; i = edge[i].n )
				if ( vis[edge[i].v] )
					++ cnt ;
		}
		cnt >>= 1 ;//每条边计数了两次,要除以二
		if ( cnt > bcc[0] )//this bcc have more than one circles
			clash_way += cnt ;
	}
	
	void tarjan ( int u , int fa ) {
		low[u] = dfn[u] = ++ dfs_clock ;
		S[top ++] = u ;
		for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
			int v = edge[i].v ;
			if ( !dfn[v] ) {
				tarjan ( v , u ) ;
				low[u] = min ( low[u] , low[v] ) ;
				if ( low[v] >= dfn[u] ) {//u is cut 
					if ( low[v] > dfn[u] )//(u,v) is bridge
						++ bridge ;
					bcc[0] = 0 ;
					while ( 1 ) {
						int x = S[-- top] ;
						bcc[++ bcc[0]] = x ;
						if ( x == v )
							break ;
					}
					bcc[++ bcc[0]] = u ;
					circuit () ;
				}
			}
			else if ( v != fa )
				low[u] = min ( low[u] , dfn[v] ) ;
		}
	}
	
	void solve ( int n ) {
		REPF ( i , 1 , n )
			if ( !dfn[i] )
				tarjan ( i , -1 ) ;
		printf ( "%d %d\n" , bridge , clash_way ) ;
	}
} ;

BCC c ;

void work () {
	int n , m ;
	int u , v ;
	while ( ~scanf ( "%d%d" , &n , &m ) && ( n || m ) ) {
		c.init () ;
		REP ( i , m ) {
			scanf ( "%d%d" , &u , &v ) ;
			c.addedge ( u , v ) ;
		}
		c.solve ( n ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: