【HDU - 5452 】Minimum Cut (树上差分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coldfresh/article/details/82223873

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

做过类似的题,关键是读错题了。。。
代码:

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#define maxx 20005
#define maxn 20
using namespace std;
int head[maxx],_next[maxx<<1],to[maxx<<1];
int cnt;
int n,m,N;
void addEdge(int x,int y)
{
    to[++cnt]=y,_next[cnt]=head[x],head[x]=cnt;
    to[++cnt]=x,_next[cnt]=head[y],head[y]=cnt;
}
int grand[maxx][maxn];
int depth[maxx];
int F[maxx];
void dfs(int root)
{
    for(int i=1;i<=N;i++)
        grand[root][i]=grand[grand[root][i-1]][i-1];
    for(int i=head[root];i;i=_next[i])
    {
        int v=to[i];
        if(v==grand[root][0])
            continue;
        grand[v][0]=root;
        depth[v]=depth[root]+1;
        dfs(v);
    }
}
int lca(int a,int b)
{
    if(depth[a]>depth[b])swap(a,b);
    for(int i=N;i>=0;i--)
        if(depth[a]<depth[b]&&depth[a]<=depth[grand[b][i]])
            b=grand[b][i];
    if(a==b)
        return a;
     for(int i=N;i>=0;i--)
     {
         if(grand[a][i]!=grand[b][i])
         {
             a=grand[a][i];
             b=grand[b][i];
         }
     }
     return grand[a][0];
}
void dfs2(int root)
{
    for(int i=head[root];i;i=_next[i])
    {
        int v=to[i];
        if(v==grand[root][0])
            continue;
        dfs2(v);
        F[root]+=F[v];//累加覆盖数,类似于线性覆盖
    }
}
void init()
{
    memset(head,0,sizeof(head));
    memset(F,0,sizeof(F));
    N=floor(log2(n));
    depth[1]=0;
    cnt=0;
}
int main()
{
    int t;
    cin>>t;
    int cal=1;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        int x,y;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            addEdge(x,y);
        }
        dfs(1);
        for(int i=n;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            F[x]++;
            F[y]++;
            F[lca(x,y)]-=2;
        }
        dfs2(1);
        int ans=m;
        for(int i=2;i<=n;i++)
            ans=min(ans,F[i]+1);
        printf("Case #%d: %d\n",cal++,ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Coldfresh/article/details/82223873
今日推荐