poj3177—Redundant Paths(缩点+边双联通分量)

来源:互联网 发布:windows安装苹方字体 编辑:程序博客网 时间:2024/05/22 00:36

题目链接:传送门

Redundant Paths
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15655 Accepted: 6573

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 71 22 33 42 54 55 65 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.


题目大意:给定现有的R条连接两个牧场的路,计算至少需要新修多少条直接连接两个牧场的路,使得任何两个牧场之至少有两条独立的路。两条独立的路指没有公共边的路,但可以经过相同的点。


解题思路:对于一个图上的双联通分量上的点肯定都是符合条件,所以我们可以将双联通分量上的点看做一个点,只要其中某一个点与点p之间至少有两条独立的路,那该联通分量上的其他点也必然与点p之间至少有两条独立的路。将原图中边联通分量上的点压缩成一个点,那么原图中的割边就是连接这些点的边,这样我们就会得到一颗树。若想使这颗树成为一个双联通的图,添加的最少边数=(树中的叶子节点数+1)/2。


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <stack>#include <queue>#include <set>#include <map>#include <vector>using namespace std;typedef long long ll;typedef pair<int,int>PA;const int N = 10090;const int M = 5090;const int INF = 0x3fffffff;const double eps = 1e-8;const double PI = acos(-1.0);//将边双联通分量上的点缩成一个点//原图中的桥为新边,建立一颗树//要使树变成边双联通图,至少添加的边数:(树中的叶子节点数+1)/2struct Edge{    int node;    Edge*next;}m_edge[N*2];Edge*head[M];int low[M],dfn[M],flag[M],Ecnt,dep;map<PA,int>mp;int sta[M];  //统计新图中的叶子节点数void init(){    Ecnt = dep = 0;    fill( head , head+M , (Edge*)0 );    fill( flag , flag+M , 0 );    mp.clear();}void mkEdge( int a , int b ){    m_edge[Ecnt].node = b;    m_edge[Ecnt].next = head[a];    head[a] = m_edge+Ecnt++;}void tarjan( int u , int father ){    flag[u] = 1;    low[u] = dfn[u] = ++dep;    for( Edge*p = head[u] ; p ; p = p->next ){        int v = p->node;        if( !flag[v] ){            tarjan( v , u );            low[u] = min( low[u] , low[v] );            if( low[v] > dfn[u] ){                mp[PA(u,v)] = 1;            }        }        if( flag[v] && v != father ){            low[u] = min( low[u] , dfn[v] );        }    }}//求边联通分量,同一个联通分量上的点的flag指向的值相同void dfs( int u , int num ){    flag[u] = num;    for( Edge*p = head[u] ; p ; p = p->next ){        int v = p->node;        if( flag[v] ) continue;        if( mp.count(PA(u,v)) == 0 && mp.count(PA(v,u)) == 0 ){            dfs( v , num );        }    }}//初始建图void Build( int m ){    int a,b;    for( int i = 0 ; i < m ; ++i ){        scanf("%d%d",&a,&b);        mkEdge(a,b);        mkEdge(b,a);    }}int solve( int n ){    int num = 0;    memset( flag , 0 , sizeof(flag) );    for( int i = 1 ; i <= n ; ++i ){        if( !flag[i] ){            num++;            dfs( i , num );        }    }    memset( sta , 0 , sizeof(sta) );    for( map<PA,int>::iterator it = mp.begin() ;    it != mp.end() ; ++it ){        PA s = it->first;        sta[flag[s.first]]++;        sta[flag[s.second]]++;    }    //记录新树中的叶子节点    int Count = 0;    for( int i = 1 ; i <= num ; ++i ){        if( sta[i] == 1 ) Count++;    }    return Count;}int main(){    int n,m;    while( ~scanf("%d%d",&n,&m) ){        init();        Build( m );        tarjan( 1 , -1 );        int ans = solve( n );        printf("%d\n",(ans+1)/2);    }    return 0;}

 
原创粉丝点击