HDU - 5452 Minimum Cut (树上差分)
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 (1≤t≤5) 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 (2≤n≤20000) and m (n−1≤m≤200000).
The following n−1 lines describe the spanning tree T and each of them contains two integers u and v corresponding to an edge.
Next m−n+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
题目大意:
给你n个点,m条边。规定前n-1条边为生成树。要求从这个生成树中删掉一条边,让这个图不连通,问加上树上这条边,最少删几条边。
解题思路:
如图
这是一棵树,如果存在一条边(3,5)(图中的虚边)
如果你想删树上的(2,5)这条边让图不连通,那么你就必须删掉(3,5),只有(2,5) (3,5) 同时被删,才能够让图不连通。同样,如果你想删除(1,2)这条边,那么你也必须删除(3,5)这条边。所以(3,5)这条虚边,会对3——5路径上所有的点贡献1.。
同样,如果有一条虚边(2,6),那么会对2-6路径上的(2,1) (1,3) (3,6)三条边都贡献1.
那么问题就转边成了:
有一个边集(除了前n-1条边,剩下的边),边集里的边对树上的边有贡献,边集里的每条边(u,v)对树的路径u—v上的所有边的贡献都是1。最后求树上权值最小的边,让这个权值加1就是答案(+1是因为还要删掉它自己)。
那么如何求呢?就是用树上差分了。
AC代码:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e4+10;
const int maxm = 2e5+10;
int n,m;
int deep[maxn];
int f[maxn][maxn];
int head[maxn];
int val[maxn];
int ans = 0x3f3f3f3f;
int cnt = 0;
struct node{
int to;
int w;
int next;
}side[maxm*2];
void init(){
memset(head,-1,sizeof(head));
memset(deep,0,sizeof(deep));
memset(val,0,sizeof(val));
ans = 0x3f3f3f3f;
cnt = 0;
}
void add(int x,int y){
side[cnt].to = y;
side[cnt].next = head[x];
head[x] = cnt++;
}
//处理深度
void dfs(int x,int fa){
deep[x] = deep[fa]+1;
f[x][0] = fa;
for(int i=1;(1<<i)<=deep[x];i++){
f[x][i] = f[f[x][i-1]][i-1];
}
for(int i=head[x];i!=-1;i=side[i].next){
int to = side[i].to;
if(to == fa) continue;
dfs(to,x);
}
return ;
}
int lca(int x,int y){
if(deep[x]<deep[y]) swap(x,y);
int cha = deep[x] - deep[y];
//变成深度一样
for(int i=0;(1<<i)<=cha;i++){
if((1<<i)&cha)
x = f[x][i];
}
if(x!=y){
for(int i = (int)log2(n);i>=0;i--){
if(f[x][i] != f[y][i]){
x = f[x][i];
y = f[y][i];
}
}
x = f[x][0];
}
return x;
}
int solve(int x,int fa){
int res = val[x];
for(int i=head[x];i!=-1;i=side[i].next){
int to = side[i].to;
if(to == fa) continue;
res += solve(to,x);
}
if(x!=1)
ans = min(ans,res);
return res;
}
int main(){
int t;
cin>>t;
int cas = 1;
while(t--){
scanf("%d%d",&n,&m);
init();
int u,v;
for(int i=1;i<=n-1;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(1,0);//处理深度
//cout<<"深度over"<<endl;
for(int i=n;i<=m;i++){
scanf("%d%d",&u,&v);
val[u]++;
val[v]++;
val[lca(u,v)]-=2;
// printf("lca = %d\n",lca(u,v));
}
solve(1,-1);
printf("Case #%d: %d\n",cas++,ans+1);
}
return 0;
}