Redundant Paths(POJ 3177)---含重边的加边双连通

题目链接

题目描述

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.

输入格式

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.

输出格式

Line 1: A single integer that is the number of new paths that must be built.

输入样例

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

输出样例

2

分析

题目大意给定一个无向图,求可以使原图成为双连通的最少加边数,如果本身就是双连通就输出0,题目保证图是连通的。
本题加边双连通类似,唯一不同的是该提可能有重边,因此在在tarjan算法中要将前驱节点该为前驱边编号即可,对于加边成双连通的原理如下:

首先,我们自然要考虑将原先图内的双连通缩成一点,因为在双连通分量内加边是无意义的。
最后,我们对于缩点后新成的图统计其度,记录度为1的点的个数ans,那么(ans+1)/2就是答案,这是因为对于任意两个度为1的点,我们将其相连就能形成一个新的双连通分量,将这个新的双连通分量缩点后,只有以下三种情况

  • 孤点,如图①
  • 孤链的头尾,如图②
  • 被包含在某条链中,如图③:

图1
在这里插入图片描述
在这里插入图片描述
因此,按照上述原则不断加边缩点,至多加(ans+1)/2条边就可以将原图变为双连通,以下是具体源码。

源程序

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 10005
#define INF 0x3f3f3f3f
using namespace std;
struct Edge{
	int v,next,id;
	Edge(){}
	Edge(int v,int next,int id):v(v),next(next),id(id){}
}edge[MAXN*2];
int EdgeCount,head[MAXN];
int n,m,cnt;
int dfn[MAXN],low[MAXN],degree[MAXN];
void addEdge(int u,int v,int id)
{
	edge[++EdgeCount]=Edge(v,head[u],id);
	head[u]=EdgeCount;
}
void init()	
{
	memset(head,0,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(degree,0,sizeof(degree));
	EdgeCount=cnt=0;
}
void tarjan(int u,int pre)
{
	dfn[u]=low[u]=++cnt;
	for(int i=head[u];i;i=edge[i].next){
		int v=edge[i].v,id=edge[i].id;
		if(!dfn[v]){	//还没访问过
			tarjan(v,id);
			low[u]=min(low[u],low[v]);
		}
		else if(pre!=id)	//访问过且v不是u的父节点所对应的前驱编号 
			low[u]=min(low[u],dfn[v]);
	}	
} 
int main()
{
	while(~scanf("%d%d",&n,&m)){
		init();	//初始化 
		for(int i=1;i<=m;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			addEdge(u,v,i);
			addEdge(v,u,i);
		}
		tarjan(1,-1);	//题目保证图连通
		for(int u=1;u<=n;u++){	//遍历每条边 
			for(int i=head[u];i;i=edge[i].next){
				int v=edge[i].v;
				if(low[u]!=low[v])	//low值不同代表这条边连接两个双连通分量 
					degree[low[v]]++; 
			}
		} 
		int ans=0;
		for(int i=1;i<=n;i++)
			if(degree[i]==1)
				ans++;
		printf("%d\n",(ans+1)/2);	//加边数 
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值