poj 3177

一个无向图,加多少跳边使得整个图是边双连通。
缩点以后找度为1点节点,用边将其两两相连即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define f first
#define s second
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const int N = 1e3 + 10;
const int M = 2e3 + 10;
int G[N][N];
int h[N],nt[M],cnt,to[M];
int n,r,a,b,num;
int dfn[N],dfs_clock,low[N],cas,d[N];
string s;
void add(int u,int v){
	to[++cnt] = v;
	nt[cnt] = h[u];
	h[u] = cnt;
}
void tarjan(int u,int fa){
	dfn[u] = low[u] = ++dfs_clock;
	for(int i = h[u] ; i ; i = nt[i]){
		int v = to[i];
		if(!dfn[v]){
			tarjan(v,u);
			low[u] = min(low[u],low[v]);
		}
		else if(v != fa) low[u] = min(low[u],dfn[v]);
	}
}
int main(){
//	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("data.in","r",stdin);
	//freopen("data.out","w",stdout);
	while(~scanf("%d%d",&n,&r)){
		memset(h,0,sizeof(h));
		cnt = 0;num = 0;dfs_clock=0;
		memset(d,0,sizeof(d));
		memset(G,0,sizeof(G));
		memset(dfn,0,sizeof(dfn));
		for(int i = 0 ; i < r; i++){
			scanf("%d%d",&a,&b);
			if(G[a][b]) continue;
			G[a][b] = G[b][a] =1;
			add(a,b);
			add(b,a);
		}
		for(int i = 1; i <= n; i++) 
			if(!dfn[i]) tarjan(i,-1);
		//for(int i = 1; i <= n;i++) cout<<dfn[i]<<' ';cout<<endl;
		//for(int i = 1; i <= n; i++) cout<<low[i]<<' ';cout<<endl;
		for(int i = 1; i <= n; i++) {
			for(int j = h[i] ; j ;j = nt[j]) {
				if(low[to[j]] != low[i]) {
					d[low[i]]++;
				}
			}
		}
		for(int i = 1; i <= n; i++) if(d[i] == 1) num++;
		printf("%d\n",(num+1)/2);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值