Minimum Cut
Minimum Cut
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 769 Accepted Submission(s): 340
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.
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.
考虑每条不属 于 T 边 对 生成树 T 树 边 的覆盖次数。
每条树边被覆盖的次数其实就是断裂这条树边后还需断裂的新边数。
用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 ;
}
最新文章
- Kubernetes集群搭建过程中遇到的问题
- 【接口测试】jmeter的使用
- JVM常见的七种垃圾收集器的简单比较
- leach和leach-c协议仿真
- 【转】Maven最佳实践:划分模块
- synzhronized原理3
- Eclipse 修改debug当前行的颜色
- zookeeper应用——集中配置管理系统的实现
- zeromq源码分析笔记之线程间收发命令(2)
- 我的Python成长之路---第三天---Python基础(9)---2016年1月16日(雾霾)
- /proc/kcore失效,调试其文件系统相关模块,使重新正常工作
- PHP设计模式概述
- 使用rsync备份与同步文件
- ubuntu17.04 调试系统工具bcc,systamtap安装
- setfacl语法2
- sqlalchemy 和 django 插入操作后自动返回自增ID
- Mac上python2和python3的版本切换
- Golang 中哪些值是不可以寻址的
- Java利用for循环输出空心的菱形
- line-height属性的深入了解