向前走莫回头❤

【POJ 3177】Redundant Paths(Tarjan求桥、边双连通分量)

Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13604   Accepted: 5782

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

[题意][求一个无向图中还需加入多少条边能构成一个边双连通分量]
【题解】【看起来很厉害的样子,其实还是先用Tarjan缩点,然后,枚举每一条边,看左右两个端点缩点后是否在同一个点中,如果不在连边,其实只要更新每点的度即可,最后统计度为1的点的个数ans,由求“加入多少条边能构成一个边双连通分量”的方法可知,答案为(ans+1)/2。详见博客Tarjan算法

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
	int u,v;
}mp[10010];
int a[20010],nxt[20010],p[5010],tot;
int dft[5010],dis[5010],f[5010],cnt,root;
int que[5010],top,size[5010],n,m,ans;
bool vis[5010];
inline void add(int x,int y)
{
	tot++; a[tot]=y; nxt[tot]=p[x]; p[x]=tot;
	tot++; a[tot]=x; nxt[tot]=p[y]; p[y]=tot;
}
void tarjan(int x,int fa)
{
	dft[x]=dis[x]=++cnt; que[++top]=x; vis[x]=1;
	bool b=0;
	for(int i=p[x];i!=-1;i=nxt[i])
	 {
	 	if(a[i]==fa&&!b) {b=1; continue;}
	    if(!dft[a[i]])
	     {
	  	    tarjan(a[i],x);
	  	    dis[x]=min(dis[x],dis[a[i]]);
	     }
	    else
	     if(vis[a[i]]&&dis[x]>dft[a[i]]) dis[x]=dft[a[i]];
	 }
	if(dis[x]==dft[x])
	 {
	 	int b; root++;
	 	do{
	 		b=que[top--];
	 		f[b]=root;
	 		vis[b]=0;
		 }while(b!=x);
	 }
}
int main()
{
	//freopen("int.txt","r",stdin);
	//freopen("my.txt","w",stdout);
	int i,j;
	memset(p,-1,sizeof(p));
	memset(nxt,-1,sizeof(nxt));
	scanf("%d%d",&n,&m);
	for(i=1;i<=m;++i)
	 {
	 	int x,y;
	 	scanf("%d%d",&x,&y);
	 	add(x,y); mp[i].u=x; mp[i].v=y;
	 }
	for(i=1;i<=n;++i)
	 if(!dft[i]) tarjan(i,0);
    for(i=1;i<=m;++i)
     {
     	int x=mp[i].u,y=mp[i].v;
     	if(f[x]!=f[y]) size[f[x]]++,size[f[y]]++;
	 }
	for(i=1;i<=root;++i)
	 if(size[i]==1) ans++;
	ans=(ans+1)/2;
	printf("%d\n",ans);
	return 0;
}


posted @ 2016-11-08 00:31  lris0-0  阅读(106)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m