Minimum Cut HDU5452 图论(记忆化搜索 + 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 (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个点,n-1条边的树,然后再给你m-n+1条边,让你求恰好删除掉一条树中的边并且让图不连通的方法中,删除的最少数目的边。
分析:
考虑让树中的一颗子树被分割下来,那我们需要计算出这颗子树需要断开多少的非树边,所以我们要便利所有的非树边,首先,对于边(u,v)来说,断开u需要一条边,断开v需要一条边,所以du[u]++,du[v]++。也就是包含u的子树度数加一,包含v的子树度数加一,但是,我们考虑,既包含u又包含v的子树是不需要断开这条边的,所以u,v的公共祖先lca(u,v)的度数要减掉2.

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
const int maxn = 20000 + 10;
const int maxm = 200000 + 10;
int n,m;
const int INF=0x3f3f3f3f;
int head[maxm*2];
int du[maxn];
struct edge
{
    int next,to;
}e[maxm*2];
int tot;
int dp[maxn];
void add(int u,int v)
{
    e[++tot].next=head[u];
    e[tot].to=v;
    head[u]=tot;
}
int dep[maxn],pa[maxn][25],far[maxn];
void dfs(int u,int f,int deep)
{
    dep[u]=deep; far[u]=f;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(v==f) continue;
        dfs(v,u,deep+1);
    }
}
void make_pa()
{
    memset(pa,-1,sizeof(pa));
    for(int i=1;i<=n;i++) pa[i][0]=far[i];
    for(int j=1;;j++)
    {
        int k=pow(2,j); if(k>n) break;
        for(int i=1;i<=n;i++)
        {
            if(pa[i][j-1]!=-1)
                pa[i][j]=pa[pa[i][j-1]][j-1];
        }
    }
}
int lca(int x,int y)
{
    int i;
    if(dep[x]<dep[y]) swap(x,y);
    for(i=16;i>=0;i--)
    {
        if(dep[x]-(1<<i)>=dep[y]) x=pa[x][i];
    }
    if(x==y) return x;
    for(i=16;i>=0;i--)
    {
        if(pa[x][i]!=-1&&pa[x][i]!=pa[y][i])
            x=pa[x][i],y=pa[y][i];
    }
    return far[x];
}
int DFS(int u,int f)
{
    int &ans=dp[u];
    ans=du[u];
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(v==f) continue;
        ans+=DFS(v,u);
    }
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    int kase=0;
    while(T--)
    {
        tot=0;
        memset(head,-1,sizeof(head));
        memset(dp,0,sizeof(dp));
        memset(du,0,sizeof(du));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n-1;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        dfs(1,1,0);
        make_pa();
        for(int i=n;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            du[u]++; du[v]++;
            int t=lca(u,v);
            du[t]-=2;
        }
        DFS(1,1);
        int ans=INF;
        for(int i=2;i<=n;i++)
        {
            ans=min(ans,dp[i]);
        }
        printf("Case #%d: %d\n",++kase,ans+1);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值