POJ 3352 Road Construction

来源:互联网 发布:ubuntu设置ipv6 dns 编辑:程序博客网 时间:2024/05/04 16:44
Road Construction
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 6949 Accepted: 3481

Description

It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 ton. Each of the followingr lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelledv andw. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

Output

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input

Sample Input 110 121 21 31 42 52 65 63 73 87 84 94 109 10Sample Input 23 31 22 31 3

Sample Output

Output for Sample Input 12Output for Sample Input 20

Source

CCC 2007
  考察点: 边双连通分量和缩点
结果为:(缩点后度为1的节点数+1)/2;
 一开始对缩点的理解有误,以为两个缩点之间只用一条线连接来,实际上不是的
/***************************************************************> File Name:    road.cpp> Author:       SDUT_GYX> Mail:         2272902662@qq.com> Created Time: 2013/5/21 0:22:56 **************************************************************/#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cstdlib>#include <string>#include <vector>#include <cmath>#define LL long longusing namespace std;vector<int> pt[1100];int low[1100],dfn[1100],degree[1100],cou,node[1100];int main(){//freopen("data1.in","r",stdin);void Tarjan(int u,int fa);int n,m;while(scanf("%d %d",&n,&m)!=EOF){for(int i=0;i<=n;i++){pt[i].clear();}for(int i=0;i<=m-1;i++){int x,y;scanf("%d %d",&x,&y);pt[x].push_back(y);pt[y].push_back(x);}memset(dfn,0,sizeof(dfn));cou=0;Tarjan(1,-1);memset(degree,0,sizeof(degree));for(int i=1;i<=n;i++){for(int j=1;j<=pt[i].size();j++){int end=pt[i][j-1];if(low[i]!=low[end]){degree[low[i]]++;degree[low[end]]++;}}} int res=0;for(int i=1;i<=n;i++){if(degree[i]/2==1){res+=1;}}printf("%d\n",(res+1)/2);}return 0;}void Tarjan(int u,int fa){low[u]=dfn[u]=++cou;for(int i=0;i<=pt[u].size()-1; i++){int v=pt[u][i];if(!dfn[v]){Tarjan(v,u);low[u]=min(low[u],low[v]);}else if(dfn[v]<dfn[u]&&v!=fa){low[u]=min(low[u],dfn[v]);}}}