Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8849 | Accepted: 3827 |
Description
Input
Output
Sample Input
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
2
Hint
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:Source
#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 ; }