Railway HDU - 3394(tarjan)

There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one tourist routes, there might be clash on it, and if a railway belongs to none tourist route, it doesn’t need to build.
Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.

Input The Input consists of multiple test cases. The first line of each test case contains two integers, n (0 < n <= 10000), m (0 <= m <= 100000), which are the number of locations and the number of the railways. The next m lines, each line contains two integers, u, v (0 <= u, v < n), which means the manger plans to build a railway on the road between u and v.
You can assume that there is no loop and no multiple edges.
The last test case is followed by two zeros on a single line, which means the end of the input. Output Output the number of railways that are no need to build, and the number of railways where clash might happen. Please follow the format as the sample. Sample Input
8 10
0 1
1 2
2 3
3 0
3 4
4 5
5 6
6 7
7 4
5 7
0 0
Sample Output
1 5

题意:有许多铁路,他们形成了许多环,现在要找出他们中不存在任何一个环中的铁路数和同时存在于多个环中的铁路数

思路:不存在于任何环中也就是无向图的桥,所以这道题求出无向图的双联通分量,找出所有的桥,就是第一个答案

存在于多个环中就是某个双联通分量中,边数>点数,那么这个双联通分量就不止一个环了,也就是双联通分量中的点都同时存在于多个环中。

那么如何判断一个双联通分量中环的个数呢?则要根据点数和边数的关系

1.点数  =  边数,形成一个环

2.点数  >  边数(一条线段,说明这条边是桥)

3.点数  < 边数 ,那么就含有1个以上的环了

代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
const int N = 10005;
struct node{
	int x,y;
	node(int a=0,int b=0){
		x=a;
		y=b;
	}
};
int LOW[N],DFN[N],index;
int vis[N];
stack<node>st;
vector<int>G[N];
int ans1,ans2;
int n,m;
void init(){
	ans1=ans2=index=0;
	memset(LOW,0,sizeof(LOW));
	memset(DFN,0,sizeof(DFN));
	for(int i=0;i<=n;i++)
	   G[i].clear();
	while(!st.empty()) st.pop();
}
void judge(int u,int v){
	memset(vis,0,sizeof(vis));
	int x,y;
	int ans=0,res=0;
	while(!st.empty()){
		node temp=st.top();
		st.pop();
		x=temp.x,y=temp.y;
		ans++;//边数 
		if(!vis[y]) res++,vis[y]=1; //点数 
		if(x==u) break;
		if(!vis[x]) res++,vis[x]=1;
	}
	if(ans>res) ans2+=ans;
}
void tarjan(int x,int fa){
	LOW[x]=DFN[x]=++index;
	for(int i=0;i<G[x].size();i++){
		int v=G[x][i];
		if(v==fa) continue;
		if(!DFN[v]&&DFN[v]<DFN[x]){
			st.push(node(x,v));
			tarjan(v,x);
			LOW[x]=min(LOW[x],LOW[v]);
			if(DFN[x]<=LOW[v]){ //判断割点 
				if(DFN[x]<LOW[v]) ans1++; //判断割边 
				judge(x,v);
			}
		}
		else if(DFN[v]<DFN[x]){
			st.push(node(x,v));
			LOW[x]=min(LOW[x],DFN[v]);
		}
	}
}
int main(){
	while(scanf("%d%d",&n,&m)!=EOF&&n+m){
		init();
		for(int i=0;i<m;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			G[x].push_back(y);
			G[y].push_back(x);
		}
		
		for(int i=0;i<n;i++)
		   if(!DFN[i]) tarjan(i,i);
		
		printf("%d %d\n",ans1,ans2);  
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值