HDU5452 Minimum Cut(图论)

Minimum Cut

传送门1
传送门2
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


题意

给出一个图 G ,求删除最少的边使的图G变为不连通,所删除的边有且仅有一条属于图 G 的生成树T.

分析

显然我们要把一个点从图中分离。那么这个点在树上的度一定为1,再取图上另外要删除的边最少的点,它的度就是另外要删除的边的条数,最后答案加1.

CODE
#include<cstdio>
#include<memory.h>
#define N 20005
int ct[N],dg[N];
int main() {
    int T;
    scanf("%d",&T);
    for(int cas=1; cas<=T; cas++) {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(ct,0,sizeof ct);
        memset(dg,0,sizeof dg);
        int u,v;
        for(int i=1; i<n; i++) {
            scanf("%d%d",&u,&v);
            dg[u]++;
            dg[v]++;
        }
        for(int i=n; i<=m; i++) {
            scanf("%d%d",&u,&v);
            ct[u]++;
            ct[v]++;
        }
        int res=2e9;
        for(int i=2; i<=n; i++)if(dg[i]==1)if(res>ct[i])res=ct[i];
        printf("Case #%d: %d\n",cas,res+1);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值