hdu5452 Minimum Cut(最近公共祖先LCA+差分前缀和)

Given a simple unweighted graph  G  (an undirected graph containing no loops nor multiple edges) with  n  nodes and  m  edges. Let  T  be a spanning tree of  G .
We say that a cut in  G  respects  T  if it cuts just one edges of  T .

Since love needs good faith and hypocrisy return for only grief, you should find the minimum cut of graph  G  respecting the given spanning tree  T .
 

Input
The input contains several test cases.
The first line of the input is a single integer  t (1t5)  which is the number of test cases.
Then  t  test cases follow.

Each test case contains several lines.
The first line contains two integers  n (2n20000)  and  m (n1m200000) .
The following  n1  lines describe the spanning tree  T  and each of them contains two integers  u  and  v  corresponding to an edge.
Next  mn+1  lines describe the undirected graph  G  and each of them contains two integers  u  and  v  corresponding to an edge which is not in the spanning tree  T .
 

Output
For each test case, you should output the minimum cut of graph  G  respecting the given spanning tree  T .
 

Sample Input
  
  
1 4 5 1 2 2 3 3 4 1 3 1 4
 

Sample Output
  
  
Case #1: 2

http://acm.hdu.edu.cn/showproblem.php?pid=5452

给定一个图的一棵生成树然后给出一些其他的边,没有重边和自环,问在取且仅取一条树边的前提下,图的最小割边的数量是多少?

割边的定义是去掉这几条边可以使连通图不连通。

感觉理解是很重要的,不管是题意还是做法。

对于每条不是树上的边<a, b>, a节点加1, b节点加1,LCA(a, b)减 2,对每颗子树求和。

首先,生成树肯定是一颗经过图中所有点的树。那么,要使图中某两点断连,去掉的边必然包括生成树中的一条或几条边。而题目要求是去且去掉一条,所以可以枚举去掉哪一条树边。

明确w[p]的定义:使p和fa[p]断开所最少需要去掉的非树边的边数。所以没有w[0]。

LCA(a, b)减 2是因为当前枚举的这条去掉的边是不能对LCA(a, b)以及它的父父父…节点造成影响的,在通过dfs函数将子节点值累加到父节点时,+1+1-2,可以消去了。

这个dfs写得真心不错,把原来要用树链剖分还是树形dp的部分直接简化了。

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
using namespace std;
vector<int> g[20005],t[20005];
int fa[20005],vis[20005],w[20005],minn;
int find(int x) {
    return x==fa[x] ? x : (fa[x] = find(fa[x]));
}
void lca(int u,int p){
	for(int i=0;i<t[u].size();++i){
		int v=t[u][i];
		if(v!=p){
			lca(v,u);
			fa[v]=u; 
		}
	}
	vis[u]=1;
	for(int i=0;i<g[u].size();++i){
		int v=g[u][i];
		if(vis[v]==1){
			w[u]++;w[v]++;
			w[find(v)]-=2;  //为什么find(u)是错的?
		}
	}
}
void dfs(int u,int p){
	for(int i=0;i<t[u].size();++i){
		int v=t[u][i];
		if(v!=p){
			dfs(v,u);
			w[u]+=w[v];
		}
	}
	if(u!=0)
		minn=min(minn,w[u]);
}
int main(){
	int tt,cnt=0;
	scanf("%d",&tt);
	while(tt--){
		minn=100000000;
		int n,m,a,b;
		scanf("%d%d", &n, &m);
		memset(w,0,sizeof(w));
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;++i)
			g[i].clear(),t[i].clear();
		for(int i=0;i<n;++i)
			fa[i]=i;
		for(int i=0;i<n-1;++i){
			scanf("%d%d", &a, &b);
			a--;b--;
			t[a].push_back(b);
			t[b].push_back(a);
		}
		for(int i=n-1;i<m;++i){
			scanf("%d%d", &a, &b);
			a--;b--;
			g[a].push_back(b);
			g[b].push_back(a);
		}
		lca(0,-1);
		dfs(0,-1);
		printf("Case #%d: %d\n",++cnt,minn+1);
	}
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值