【POJ】3177 Redundant Paths 边连通

Redundant Paths
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 8849
Accepted: 3827

Description

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2

Hint

Explanation of the sample:

One visualization of the paths is:
   1   2   3
   +---+---+  
       |   |
       |   |
 6 +---+---+ 4
      / 5
     / 
    / 
 7 +
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
   1   2   3
   +---+---+  
   :   |   |
   :   |   |
 6 +---+---+ 4
      / 5  :
     /     :
    /      :
 7 + - - - - 
Check some of the routes:
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7

Every pair of fields is, in fact, connected by two routes.

It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

Source

USACO 2006 January Gold

传送门:【POJ】3177 Redundant Paths

题目分析:求出边双连通然后缩点,找出度为1的分量个数ans,答案即(ans+1) / 2。

代码如下:

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


#define up( i ) ( ( i ) << 1 )
#define down( i ) ( ( i ) << 1 | 1 )
#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 = 5005 ;
const int MAXE = 10005 ;
const int INF = 0x7f7f7f7f ;

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

struct Line {
	int u , v ;
	void input () {
		scanf ( "%d%d" , &u , &v ) ;
	}
} ;

struct BCC {
	Line line[MAXE] ;
	Edge edge[MAXE << 1] ;
	int adj[MAXN] , cntE ;
	int bcc[MAXN] , bcc_cnt ;
	int low[MAXN] , dfn[MAXN] , dfs_clock ;
	int S[MAXN] , top ;
	int deg[MAXN] ;
	int n , m ;
	
	void init () {
		cntE = top = bcc_cnt = dfs_clock = 0 ;
		clear ( dfn , 0 ) ;
		clear ( deg , 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 ;
		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
				low[u] = min ( low[u] , dfn[v] ) ;
		}				
		if ( low[u] == dfn[u] ) {
			++ bcc_cnt ;
			while ( 1 ) {
				int v = S[-- top] ;
				bcc[v] = bcc_cnt ;
				if ( v == u )
					break ;
			}
		}
	}
	
	void input () {
		REP ( i , m ) {
			line[i].input () ;
			addedge ( line[i].u , line[i].v ) ;
		}
	}
	
	void solve () {
		tarjan ( 1 , -1 ) ;
		REP ( i , m ) {
			int u = bcc[line[i].u] ;
			int v = bcc[line[i].v] ;
			if ( u != v )
				++ deg[u] , ++ deg[v] ;
		}
		int ans = 0 ;
		REPF ( i , 1 , bcc_cnt )
			if ( deg[i] == 1 )
				++ ans ;
		printf ( "%d\n" , ( ans + 1 ) >> 1 ) ;
	}
} ;

BCC z ;

void work () {
	int n , m ;
	while ( ~scanf ( "%d%d" , &z.n , &z.m ) ) {
		z.init () ;
		z.input () ;
		z.solve () ;
	}
}

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


你可能感兴趣的:(poj)