现在的位置: 首页 > 综合 > 正文

【HDU】2460 Network 双连通+LCA变形

2017年10月15日 ⁄ 综合 ⁄ 共 4066字 ⁄ 字号 评论关闭

Network

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 749    Accepted Submission(s): 149

Problem Description

A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links,
so data can be transformed between any two computers. The administrator finds that some links are vital to the network, because failure of any one of them can cause that data can't be transformed between some computers. He call such a link a bridge. He is
planning to add some new links one by one to eliminate all bridges.

You are to help the administrator by reporting the number of bridges in the network after each new link is added.

 


Input

The input consists of multiple test cases. Each test case starts with a line containing two integers N(1 ≤ N ≤ 100,000) and M(N - 1 ≤ M ≤ 200,000).
Each of the following M lines contains two integers A and B ( 1≤ A ≠ B ≤ N), which indicates a link between computer A and B. Computers are numbered from 1 to N. It is guaranteed that any two computers are connected in the initial network.
The next line contains a single integer Q ( 1 ≤ Q ≤ 1,000), which is the number of new links the administrator plans to add to the network one by one.
The i-th line of the following Q lines contains two integer A and B (1 ≤ A ≠ B ≤ N), which is the i-th added new link connecting computer A and B.

The last test case is followed by a line containing two zeros.

 


Output

For each test case, print a line containing the test case number( beginning with 1) and Q lines, the i-th of which contains a integer indicating the number of bridges in the network after the first i new
links are added. Print a blank line after the output for each test case.
 


Sample Input
3 2 1 2 2 3 2 1 2 1 3 4 4 1 2 2 1 2 3 1 4 2 1 2 3 4 0 0
 


Sample Output
Case 1: 1 0 Case 2: 2 0
 


Source

2008 Asia Hefei Regional Contest Online
by USTC

传送门:【HDU】2460 Network

题目分析:首先tarjan求出双连通分量,然后重新建边,dfs将无根树转化为有根树,记录父亲、节点深度,并且记录桥的数量。然后每次询问非递归的查找询问两点的最近公共祖先,根据深度平衡两端的抬升,直到遇到最近公共祖先(代码中则是 u == v )抬升节点的同时,将路过的节点压栈,同时减去路过的边的数量(一定是桥),剩下桥的数量就是减去后的个数,然后对所有在栈中的节点全部指向最近公共祖先(等效于路径压缩)。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#pragma comment(linker,"/STACk:10240000,10240000")
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 )

typedef long long LL ;

const int MAXN = 100005 ;
const int MAXE = 200005 ;

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] , bcc_cnt ;
	int ins[MAXN] , S[MAXN] , dfs_clock , top ;
	int low[MAXN] , dfn[MAXN] ;
	
	void init () {
		top = cntE = dfs_clock = bcc_cnt = 0 ;
		clear ( ins , 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 tarjan ( int u , int fa ) {
		low[u] = dfn[u] = ++ dfs_clock ;
		ins[u] = 1 ;
		S[top ++] = u ;
		int flag = 1 ;
		for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
			int v = edge[i].v ;
			if ( v == fa && flag ) {
				flag = 0 ;
				continue ;
			}
			if ( !dfn[v] ) {
				tarjan ( v , u ) ;
				low[u] = min ( low[u] , low[v] ) ;
			}
			else if ( ins[v] )
				low[u] = min ( low[u] , dfn[v] ) ;
		}
		if ( low[u] == dfn[u] ) {
			++ bcc_cnt ;
			while ( 1 ) {
				int v = S[-- top] ;
				ins[v] = 0 ;
				bcc[v] = bcc_cnt ;
				if ( v == u )
					break ;
			}
		}
	}
	
	void find_bcc ( int n ) {
		REPF ( i , 1 , n )
			if ( !dfn[i] )
				tarjan ( i , -1 ) ;
	}
} ;

struct LCA {
	Edge edge[MAXE << 1] ;
	int adj[MAXN] , cntE ;
	int deep[MAXN] ;
	int p[MAXN] ;
	int pre[MAXN] ;
	int tot ;
	int S[MAXN] , top ;
	
	void init () {
		cntE = 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 ++ ;
	}
	
	int find ( int x ) {
		return p[x] == x ? x : ( p[x] = find ( p[x] ) ) ;
	}
	
	void dfs ( int u , int fa ) {
		pre[u] = u ;
		for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
			int v = edge[i].v ;
			if ( v == fa )
				continue ;
			++ tot ;
			deep[v] = deep[u] + 1 ;
			dfs ( v , u ) ;
			pre[v] = u ;
		}
	}
	
	void preProcess ( int n ) {
		REPF ( i , 1 , n )
			p[i] = i ;
		tot = 0 ;
		deep[1] = 0 ;
		dfs ( 1 , 0 ) ;
	}
	
	void solve ( int u , int v ) {
		u = find ( u ) ;
		v = find ( v ) ;
		top = 0 ;
		while ( u != v ) {
			if ( deep[u] > deep[v] )
				S[top ++] = u , u = find ( pre[u] ) ;
			else
				S[top ++] = v , v = find ( pre[v] ) ;
			-- tot ;
		}
		while ( top )
			p[S[-- top]] = u ;
		printf ( "%d\n" , tot ) ;
	}
} ;	

BCC c ;
LCA a ;
int uu[MAXE] , vv[MAXE] ;

void work () {
	int cas = 0 ;
	int n , m ;
	int u , v ;
	while ( ~scanf ( "%d%d" , &n , &m ) && ( n || m ) ) {
		printf ( "Case %d:\n" , ++ cas ) ;
		c.init () ;
		a.init () ;
		REP ( i , m ) {
			scanf ( "%d%d" , &uu[i] , &vv[i] ) ;
			c.addedge ( uu[i] , vv[i] ) ;
		}
		c.find_bcc ( n ) ;
		REP ( i , m )
			if ( c.bcc[uu[i]] != c.bcc[vv[i]] )
				a.addedge ( c.bcc[uu[i]] , c.bcc[vv[i]] ) ;
		a.preProcess ( c.bcc_cnt ) ;
		scanf ( "%d" , &m ) ;
		while ( m -- ) {
			scanf ( "%d%d" , &u , &v ) ;
			a.solve ( c.bcc[u] , c.bcc[v] ) ;
		}
		printf ( "\n" ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}

抱歉!评论已关闭.