poj 3177 Redundant Paths 【无向图增加最少的边是图成为边—双连通】【tarjan求EBC + 缩点 统计度数为1的EBC】


Redundant Paths
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10494 Accepted: 4503

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.


题意:问在图中最少增加几条边使图成为边-双连通


思路:求出所有EBC,缩点后计算EBC的度数, 求出度数为1的EBC的总数sum,答案就是(sum + 1) / 2。


AC代码:


#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <algorithm>
#define MAXN 5000+10
#define MAXM 25000000+10
#define INF 10000000
using namespace std;
struct Edge
{
	int from, to, next; 
}edge[MAXM];
int head[MAXN], edgenum;
int low[MAXN], dfn[MAXN];
int dfs_clock;
int ebcno[MAXN], ebc_cnt;
vector<int> ebc[MAXN];
stack<int> S;
bool Instack[MAXN];
int n, m;//n个点 m条边 
int du[MAXN];//记录ebc度数 
void init()
{
	edgenum = 0;
	memset(head, -1, sizeof(head));
} 
void addEdge(int u, int v)
{
	Edge E = {u, v, head[u]};
	edge[edgenum] = E;
	head[u] = edgenum++;
}
void getMap()
{
	int a, b;
	while(m--)
	{
		scanf("%d%d", &a, &b);
		addEdge(a, b);
		addEdge(b, a);
	}
}
void tarjan(int u, int fa)
{
	int v;
	low[u] = dfn[u] = ++dfs_clock;
	S.push(u);
	Instack[u] = true;
	int have = 1;
	for(int i = head[u]; i != -1; i = edge[i].next)
	{
		v = edge[i].to;
		if(have && v == fa)
		{
			have = 0;
			continue;
		}
		if(!dfn[v])
		{
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
		}
		else if(Instack[v])
		low[u] = min(low[u], dfn[v]);
	}
	if(low[u] == dfn[u])
	{
		ebc_cnt++;
		ebc[ebc_cnt].clear();
		for(;;)
		{
			v = S.top(); S.pop();
			Instack[v] = false;
			ebcno[v] = ebc_cnt;
			if(v == u) break;
		}
	}
}
void suodian()
{
	for(int i = 1; i <= ebc_cnt; i++) du[i] = 0;
	for(int i = 0; i < edgenum; i += 2)//缩点 去掉重边 
	{
		int u = ebcno[edge[i].from];
		int v = ebcno[edge[i].to];
		if(u != v)
		du[u]++, du[v]++; 
	}
}
void find_cut(int l, int r)
{
	memset(low, 0, sizeof(low));
	memset(dfn, 0, sizeof(dfn));
	memset(ebcno, 0, sizeof(ebcno));
	memset(Instack, false, sizeof(Instack));
	dfs_clock = ebc_cnt = 0;
	for(int i = l; i <= r; i++)
	if(!dfn[i]) tarjan(i, -1);
}
void solve()
{
	int sum = 0;
	if(ebc_cnt == 1)
	{
		printf("0\n");
		return ;
	}
	for(int i = 1; i <= ebc_cnt; i++) if(du[i] == 1) sum++;
	printf("%d\n", (sum+1) / 2);
}
int main()
{
	while(scanf("%d%d", &n, &m) != EOF)
	{
		init();
		getMap();
		find_cut(1, n);
		suodian();
		solve();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值