UESTC Training for Graph Theory——H、Redundant Paths

Redundant Paths

Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 39 Tried: 158

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 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: 



Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions. 



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.

Source

USACO 2006 January Gold

/*算法思想:
  给一个无向图,问至少添加多少条边才能使得每两个点之间存在至少两条路径相互到达
  首先可以把原图中的联通分量缩成一个点,这样,原图实际上就变成了一棵树,我们找到树上的叶子
  节点的个数sum,最终我们要添加的边的个数就是 (sum+1)/2 ,这样问题就变成了怎么找原图中缩点
  后叶子节点的个数。观察到,缩点的后,还保留在图中的边,一定是原图中的桥,新图的即缩点后得
  到的树就是由这些桥构成的,那么问题再次转化成了找原图中的桥,我们可以用tarjan找出原图中的
  桥,在找桥的图中顺便把在一个联通分量中的点缩成一个点,缩点可以用并查集来做。最后我们统计
  每个桥的端点的度数,度数是1,这个端点是叶子节点,sum+1
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 5005
using namespace std;
int fa[N];
struct data
{
	int en,val,next;
} edge[N*4];
int head[N],tot,low[N],dfn[N],bridge[N][3],d[N],n,m,Time;
int find_set(int x)
{
	if(x!=fa[x])
		fa[x]=find_set(fa[x]);
	return fa[x];
}
void add_edge(int st, int en)
{
	edge[tot].en=en;
	edge[tot].next=head[st];
	head[st]=tot++;
}
void find_bridge(int v,int from)  //tarjan找桥
{
	low[v]=dfn[v]=Time++;
	for(int i=head[v];i!=-1;i=edge[i].next)
	{
		if(dfn[edge[i].en]<0)
		{
			find_bridge(edge[i].en,v);
			low[v]=min(low[v],low[edge[i].en]);
			if(low[edge[i].en]<=dfn[v])
			{
				int f1=find_set(edge[i].en);
				int f2=find_set(v);
				if(f1!=f2) fa[f1]=f2;
			}
			else
			{
			    tot++;
				bridge[tot][0]=v;
				bridge[tot][1]=edge[i].en;
			}
		}
		else if(edge[i].en!=from && low[v]>dfn[edge[i].en])
            low[v]=dfn[edge[i].en];
	}
}
int main()
{
	tot=0;
	memset(head,-1,sizeof(head));
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++)
	{
	    int a,b;
		scanf("%d%d",&a,&b);
		add_edge(a,b);
		add_edge(b,a);
	}
    for(int i=1;i<=n;i++)
		fa[i]=i;
	memset(dfn,-1,sizeof(dfn));
	memset(low,0,sizeof(low));
	Time=1; tot=0;
	find_bridge(1,-1);
	int sum=0;
	memset(d,0,sizeof(d));
	for(int i=1;i<=tot;i++)
	{
		int f1=find_set(bridge[i][0]);
		int f2=find_set(bridge[i][1]);
		d[f1]++;
		d[f2]++;
	}
	for(int i=1;i<=n;i++)
		if(d[i]==1) sum++;
	printf("%d\n",(sum+1)/2);
	return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
互联网络程序设计是指在互联网上进行程序开发和设计的过程。UESTC则是我国的一所著名高校——电子科技大学。 互联网络程序设计 uestc包含了两个主要的方面:互联网络和程序设计。互联网络是指将多个计算机网络通过通信链路互相连接起来,实现信息共享和资源共享的网络系统。程序设计是指根据需求和目标,通过编写代码和设计算法,实现计算机程序的过程。 互联网络程序设计 uestc的学习内容主要包括以下几个方面: 1. 网络知识:学习互联网络的基本概念、原理和协议,如TCP/IP协议、HTTP协议等。掌握网络编程的基本技术,能够编写网络应用程序。 2. 数据通信:学习数据通信的基本原理和技术,包括数据传输的方式、数据压缩和加密等。了解网络安全和数据保护的基本知识。 3. 程序设计:学习编程语言和开发工具,如Java、C++和Python等。掌握常用的编程技巧和方法,能够设计和实现复杂的网络应用程序。 4. Web开发:学习Web开发的基本知识和技术,包括HTML、CSS、JavaScript等。能够设计和实现交互式的Web应用程序。 5. 数据库技术:学习数据库的基本原理和技术,如SQL语言和数据库管理系统。能够设计和管理数据库,实现数据的存储和检索。 通过学习互联网络程序设计 uestc,可以掌握互联网应用开发的基本技能,具备设计和实现网络应用程序的能力。这对于目前互联网行业的人才需求来说是非常重要的,也为学生提供了广阔的就业和创业机会。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值