ACM: 图论题 poj 3352 双连通题

来源:互联网 发布:win7如何安装linux系统 编辑:程序博客网 时间:2024/05/17 17:44
RoadConstruction

 

Description

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

The roads themselves are also rather interesting. Due to thestrange customs of the island, the roads are arranged so that theynever meet at intersections, but rather pass over or under eachother using bridges and tunnels. In this way, each road runsbetween two specific tourist attractions, so that the tourists donot become irreparably lost.

Unfortunately, given the nature of the repairs and upgradesneeded on each road, when the construction company works on aparticular road, it is unusable in either direction. This couldcause a problem if it becomes impossible to travel between twotourist attractions, even if the construction company works on onlyone road at any particular time.

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

Input

The first line of input will consist of positive integersn 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 areconveniently labelled from 1 to n. Each of the followingr lines will consist of two integers, v and w,separated by a space, indicating that a road exists between theattractions labelled v and w. Note that you maytravel in either direction down each road, and any pair of touristattractions will have at most one road directly between them. Also,you are assured that in the current configuration, it is possibleto travel between any two tourist attractions.

Output

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

Sample Input

Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10

Sample Input 2
3 3
1 2
2 3
1 3

Sample Output

Output for Sample Input 1
2

Output for Sample Input 2
0

题意: 道路修建, 要求添加最少的道路, 使得在整个无向图图中, 当其中一条边断开之后,

     整个图还是联通的. 求出添加最少的边数.

 

解题思路:

      1.依然采用连通分量收缩成一个整体. 如下图:(样例一的图与解, 右图2条红色边即是添加)

        ACM: <wbr>图论题 <wbr>poj <wbr>3352 <wbr>双连通题

       2.求出连通分量, 把它们看成叶子节点, 叶子节点两两组合. 是整个图变成双连通.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 1005

struct node
{
 int u, v;
 int next;
}edges[MAX*3];

int n, m;
int first[MAX], num;
int stack[MAX], top, low[MAX], dfn[MAX], count;
int degree[MAX], bcc[MAX];
bool vis[MAX];

inline int min(int a, int b)
{
 return a < b ? a : b;
}

inline void add(int u, int v)
{
 edges[num].u = u;
 edges[num].v = v;
 edges[num].next = first[u];
 first[u] = num++;
}

void readGraph()
{
 top = num = count = 0;
 memset(first, -1, sizeof(first));
 memset(stack, 0, sizeof(stack));
 memset(low, 0, sizeof(low));
 memset(dfn, 0, sizeof(dfn));
 memset(degree, 0, sizeof(degree));
 memset(vis, false, sizeof(vis));
 memset(bcc, 0, sizeof(bcc));
 int u, v;
 for(int i = 0; i < m; ++i)
 {
  scanf("%d %d",&u, &v);
  add(u, v);
  add(v, u);
 }
}

void tarjan(int u, int fa, int deep)
{
 dfn[u] = low[u] = deep;
 vis[u] = true;
 stack[top++] = u;
 for(int e = first[u]; e != -1; e =edges[e].next)
 {
  int v = edges[e].v;
  if(v != fa)
  {
   if( !low[v])
   {
    tarjan(v,u, deep+1);
    low[u]= min(low[u], low[v]);
   }
   else if(vis[v] )
    low[u]= min(low[u], dfn[v]);
  }
 }

 if(low[u] == dfn[u])
 {
  count++;
  int v;
  do
  {
   v =stack[--top];
   vis[v] =false;
   bcc[v] =count;
  }while( v != u );
 }
}

int solve()
{
 int i, result = 0;
 for(i = 1; i <= n; ++i)
 {
  if( !low[i] )
   tarjan(i, 0,1);
 }

 int u, v;
 for(i = 0; i < num; i += 2)
 {
  u = edges[i].u;
  v = edges[i].v;
  if( bcc[u] != bcc[v] )
  {
   degree[bcc[u] ]++;
   degree[bcc[v] ]++;
  }
 }

 for(i = 1; i <= count;++i)
 {
  if( degree[i] == 1)
   result++;
 }
 return (result+1)/2;
}

int main()
{
// freopen("input.txt", "r", stdin);
 while(scanf("%d %d", &n,&m) != EOF)
 {
  readGraph();
  int result = solve();
  printf("%d\n", result);
 }
 return 0;
}

0 0
原创粉丝点击