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

题意:给你一个无向连通图,问你至少需要添加几条边能使得该图是一个边双连通图?

思路:和之前的poj3352思路一样,首先我们用DFS求出图中的所有的边双连通分量(对于low[i]值不同的点必然属于不同的分量),然后我们把每个分量看成一个(缩)点,就得到了一个缩点树.要使得这颗树变成一个边双连通的,需要添加的边数=(度为1的点数目+1)/2.

Trick:这题有重边,先要去重


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 5005
#define LL long long
int cas=1,T;
int n,m;
int sum = 0;
bool graph[maxn][maxn];
int degree[maxn];
int dfs_clock;            //时钟,每访问一个结点增1
vector<int>G[maxn];       //图
int pre[maxn];            //pre[i]表示i结点被第一次访问到的时间戳,若pre[i]==0表示还未被访问
int low[maxn];            //low[i]表示i结点及其后代能通过反向边连回的最早的祖先的pre值
bool iscut[maxn];         //标记i结点是不是一个割点
int cut[maxn];            //切割这个结点后的儿子数
//求出以u为根节点(u在DFS树中的父节点是fa)的树的所有割点和桥
//初始调用dfs(root,-1)
int dfs(int u,int fa)
{
	int lowu=pre[u]=++dfs_clock;
	int child = 0;                //子结点数目
	for (int i = 0;i<G[u].size();i++)
	{
		int v = G[u][i];
		
		if (!pre[v])
		{
			child++;              //未访问过的结点才能算是u的孩子
			int lowv = dfs(v,u);
			lowu = min(lowu,lowv);
		/*	if (lowv >=pre[u])
			{
				iscut[u]=1;           //u是割点
				cut[u]++;
				if (lowv > pre[u])       //(u,v)边时桥
					printf("qiao")
			}*/
		}
		else if (pre[v] <pre[u] && v!=fa)  //v!=fa确保了(u,v)是从u到v的反向边
		{
			lowu = min(lowu,pre[v]);
		}
	}
	/*if (fa <0)   //若u是根
	{
		cut[u]--;
	}*/
	return low[u]=lowu;
}
void init()
{
	dfs_clock = 0;
	sum=0;
	memset(pre,0,sizeof(pre));
//	memset(iscut,0,sizeof(iscut));
	memset(cut,0,sizeof(cut));
	memset(degree,0,sizeof(degree));
	for (int i = 0;i<=n;i++)
		G[i].clear();
	memset(graph,false,sizeof(graph));
}
int main()
{
    scanf("%d%d",&n,&m);
	init();
	for (int i = 0;i<m;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		if (graph[u][v]==false && graph[v][u]==false)
		{
		  G[u].push_back(v);
		  G[v].push_back(u);
          graph[u][v]=true;
		  graph[v][u]=true;
		}
	}
	dfs(1,-1);                 //求出所有结点的low值,每个不同的low值代表一个边双连通分量
    for (int i = 1;i<=n;i++)
	{
		for (int j = 0;j<G[i].size();j++)
		{
			int v = G[i][j];
			if (low[i]!=low[v])
				degree[low[v]]++;
		}
	}
	int cnt = 0;
	for (int i = 1;i<=n;i++)
		if (degree[i]==1)
			cnt++;
	printf("%d\n",(cnt+1)/2);
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}


题目

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 descri_ption 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 7
1 2
2 3
3 4
2 5
4 5
5 6
5 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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值