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 ;
}
Minimum Cut的更多相关文章
- POJ Minimum Cut
Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 9302 Accepted: 3902 Case ...
- POJ 2914 Minimum Cut
Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 9319 Accepted: 3910 Case ...
- hdu 5452 Minimum Cut 树形dp
Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...
- POJ 2914 Minimum Cut 最小割图论
Description Given an undirected graph, in which two vertices can be connected by multiple edges, wha ...
- HDU 6214.Smallest Minimum Cut 最少边数最小割
Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- HDU 6214 Smallest Minimum Cut(最少边最小割)
Problem Description Consider a network G=(V,E) with source s and sink t. An s-t cut is a partition o ...
- Smallest Minimum Cut HDU - 6214(最小割集)
Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
- HDU - 6214:Smallest Minimum Cut(最小割边最小割)
Consider a network G=(V,E) G=(V,E) with source s s and sink t t . An s-t cut is a partition of nodes ...
- hdu 6214 Smallest Minimum Cut[最大流]
hdu 6214 Smallest Minimum Cut[最大流] 题意:求最小割中最少的边数. 题解:对边权乘个比边大点的数比如300,再加1 ,最后,最大流对300取余就是边数啦.. #incl ...
- HDU 6214 Smallest Minimum Cut 【网络流最小割+ 二种方法只能一种有效+hdu 3987原题】
Problem Description Consider a network G=(V,E) with source s and sink t . An s-t cut is a partition ...
随机推荐
- kafka学习(七)
跨集群数据镜像 跨集群镜像的使用场景 1.区域集群和中心集群 2.冗余,发生紧急情况下使用第二个集群,保存相同的数据. 3.云迁移 多集群架构 跨集群中心通信的一些现实情况 1.高延迟 2.有 ...
- powershell下载网站图片
$picurl = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=10" $data = ...
- eclipse 或 STS 卸载SVN 插件
help菜单 ==> about eclipse ==>install details按钮 ==> installed software选项卡 选中下面的这几项,点击 uni ...
- 关于java范型
1 范型只在编译阶段有效 编译器在编译阶段检查范型结果之后,就会将范型信息删除.范型信息不会进入运行时阶段. 泛型类型在逻辑上看以看成是多个不同的类型,实际上都是相同的基本类型. 2 不能对确定的范型 ...
- oracle跟SQL Server 2005 的区别
Oracle与Sql server的区别 一直搞不明白Oracle数据库和sql server的区别,今天我特意查资料把他们的区别整理出来 Oracle数据库:Oracle Database,又名 ...
- bfs(同一最短路径)
http://oj.jxust.edu.cn/contest/Problem?id=1702&pid=7 题意:现在有两个相同大小的地图,左上角为起点,右下角问终点.问是否存在同一条最短路径. ...
- 【WPS/Visio】WPS word无法复制或编辑Visio对象
前言 Win10,WPS2019,Visio2016. 好像是有一次设置 .vsdx 的默认打开方式为Visio,之后每次在WPS里复制Visio对象,或双击编辑WPS word中以前的Visio对象 ...
- 3.golang 的注释
package main import ( "fmt" "math" ) func main() { fmt.Println(pi(5000)) } // pi ...
- shopnc如何配置微信支付和支付宝支付
步骤一,支付宝账号申请 申请支付宝商家账号 ,填写好公司名称,资质,审核过了,然后填写下面这些参数 步骤二 微信支付申请 登陆微信公众平台-企业微信支付,得到商户号,再申请密钥 注意:支付宝加密方式 ...
- Centos 7 Mysql 最大连接数超了问题解决
错误:Can not connect to MySQL server. Too many connections -mysql 1040错误 这是因为对 Mysql 进行访问,未释放的连接数已经达到 ...