Minimum Cut

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 769    Accepted Submission(s): 340

Problem Description
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
 
Source
 
题意:在G中有T,问G 最小割中有且仅有一割在 T  中的最小割是多少。
 

考虑每条不属 于 T   边  对 生成树 T  树 边 的覆盖次数。

每条树边被覆盖的次数其实就是断裂这条树边后还需断裂的新边数。

 
在m-n-1行中都是T中没有的边,添加了(u, v)之后,v中的子节点,叶子节点都可以和u联系,那么在求最小割的时候就要删除这条边,使得G中 变成两个由 u 和v 构成的不连通的图。

用cnt 存该点需要被断裂几次,即断裂这条边后 还需要断裂几条边可以使G 不连通。遍历求最小值即最小割

 #include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring> using namespace std; #define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define N 100005 vector< vector<int> > G;
int deep[N], f[N], cnt[N]; void dfs(int u, int fa, int step)
{
deep[u] = step;
cnt[u] = ;
f[u] = fa;
int len = G[u].size();
for(int i = ; i < len; i++)
{
int v = G[u][i];
if(v != fa)
dfs(v, u, step+);
}
} void Lca(int x, int y)
{
while(x != y)
{
if(deep[x] >= deep[y])
{
cnt[x]++;
x = f[x];
}
else
{
cnt[y]++;
y = f[y];
}
}
} int main()
{
int t, i, a, b, n, m, l = ;
scanf("%d", &t);
while(t--)
{
G.resize(N);
G.clear();
scanf("%d%d", &n, &m);
for(i = ; i < n; i++)
{
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
dfs(, , );
for(; i <= m; i++)
{
scanf("%d%d", &a, &b);
Lca(a, b);
}
int ans = INF;
for(i = ; i <= n; i++)
ans = min(ans, cnt[i]);
printf("Case #%d: %d\n", l++, ans);
}
return ;
}

最新文章

  1. Kubernetes集群搭建过程中遇到的问题
  2. 【接口测试】jmeter的使用
  3. JVM常见的七种垃圾收集器的简单比较
  4. leach和leach-c协议仿真
  5. 【转】Maven最佳实践:划分模块
  6. synzhronized原理3
  7. Eclipse 修改debug当前行的颜色
  8. zookeeper应用——集中配置管理系统的实现
  9. zeromq源码分析笔记之线程间收发命令(2)
  10. 我的Python成长之路---第三天---Python基础(9)---2016年1月16日(雾霾)
  11. /proc/kcore失效,调试其文件系统相关模块,使重新正常工作
  12. PHP设计模式概述
  13. 使用rsync备份与同步文件
  14. ubuntu17.04 调试系统工具bcc,systamtap安装
  15. setfacl语法2
  16. sqlalchemy 和 django 插入操作后自动返回自增ID
  17. Mac上python2和python3的版本切换
  18. Golang 中哪些值是不可以寻址的
  19. Java利用for循环输出空心的菱形
  20. line-height属性的深入了解

热门文章

  1. android开发错误经验总结
  2. kafka消费者脚本无法启动问题
  3. 剑指Offer编程题(Java实现)——删除链表中重复的结点
  4. java面向对象详细全面介绍
  5. 断开ssh链接在后台继续运行命令
  6. QRCode.js一个生成二维码的javascript库
  7. k3 cloud中如何把一个账套中的单据部署到另一个账套中
  8. Java List对象集合按对象属性分组、分组汇总、过滤等操作示例
  9. Linux 设置定时清除buff/cache的脚本
  10. linux Apache 日志轮询