poj 3352 Road Construction(边双连通)
2012-11-13 16:50
357 查看
题意:一个国家有N个城市,有M条路连接这N个城市,通过这些路任意两个城市都可以互达,现在为了游客的安全,决定重新修葺一下这些路,但是为了能方便游客,需要新修一些路,使得任意两城市间都有两条路。
思路:先求强连通分支,然后缩点,这样就形成了一个树,找到叶子节点ans,只要修(ans+1)/2条路就可以达到目的了。
代码:
View Code
思路:先求强连通分支,然后缩点,这样就形成了一个树,找到叶子节点ans,只要修(ans+1)/2条路就可以达到目的了。
代码:
View Code
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <algorithm> #include <queue> #include <math.h> #include <stack> #define N 1005 using namespace std ; struct node { int e , next ; }p[2*N]; int dfn , low , out , head ; int n , m , cnt , id , num , ans ; //求强连通分支 void dfs( int x , int fa ) { dfn[x] = low[x] = ++id ; for ( int i = head[x] ; i != -1 ; i = p[i].next ) { int v = p[i].e ; if ( v != fa && !dfn[v] ) { dfs( v , x ) ; low[x] = min( low[x] , low[v]); if ( low[v] > dfn[x] ) { cnt++; } } else if ( v != fa && low[x] > dfn[v] ) low[x] = dfn[v] ; } } int main() { int i , j , x , y ; while ( scanf ( "%d%d" , &n , &m ) != EOF ) { //建图 memset( head , -1 , sizeof( head )) ; num = 0 ; for ( i = 1 ; i <= m ; i++ ) { scanf ( "%d%d" , &x , &y ) ; p[num].e = y ; p[num].next = head[x] ; head[x] = num++ ; p[num].e = x ; p[num].next = head[y] ; head[y] = num++ ; } //初始化 cnt = id = ans = 0 ; memset( out , 0 , sizeof ( out )) ; //缩点 dfs ( 1 , -1 ) ; //找叶子节点,度为1的为叶子节点 for ( i = 1 ; i <= n ; i++ ) { for ( j = head[i] ; j != -1 ; j = p[j].next ) { int v = p[j].e ; if ( low[i] != low[v] ) { out[low[i]]++ ; } } } for ( i = 1 ; i <= n ; i++) if ( out[i] == 1 ) ans++ ; printf ( "%d\n" , ( ans + 1 ) / 2 ) ; } return 0 ; }
相关文章推荐
- POJ-3352 Road Construction,tarjan缩点求边双连通!
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
- poj 3352 Road Construction 边双连通缩点
- POJ 3352--Road Construction【无向图增加最少的边成为边双连通图 && tarjan求ebc && 缩点构造缩点树】
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)
- poj3352--E - Road Construction(无向图加边成双连通)
- POJ 3352 Road Construction (边双连通,缩点)
- poj3352——Road Construction——————【加边为边-双连通图】
- POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)
- poj 3352 Road Construction (双连通)
- POJ 3352 Road Construction(添最少边构造边双连通图的结论)
- poj -- 3352 Road Construction(边双连通)
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告
- POJ 3352 Road Construction【双连通分量_桥】
- 【POJ 3352】 Road Construction(边联通分量入门)
- POJ 3352 Road Construction 边双联通分量
- poj 3352 边双连通
- POJ 3352 Road Construction&& POJ 3177 Redundant Paths 双联通分量
- POJ---3352-Road Construction(双连通分量)
- poj 3352--Road Construction(双连通分量)