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 ;
}

Minimum Cut的更多相关文章

  1. POJ Minimum Cut

    Minimum Cut Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 9302   Accepted: 3902 Case ...

  2. POJ 2914 Minimum Cut

    Minimum Cut Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 9319   Accepted: 3910 Case ...

  3. hdu 5452 Minimum Cut 树形dp

    Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=54 ...

  4. POJ 2914 Minimum Cut 最小割图论

    Description Given an undirected graph, in which two vertices can be connected by multiple edges, wha ...

  5. HDU 6214.Smallest Minimum Cut 最少边数最小割

    Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Oth ...

  6. 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 ...

  7. Smallest Minimum Cut HDU - 6214(最小割集)

    Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Oth ...

  8. 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 ...

  9. hdu 6214 Smallest Minimum Cut[最大流]

    hdu 6214 Smallest Minimum Cut[最大流] 题意:求最小割中最少的边数. 题解:对边权乘个比边大点的数比如300,再加1 ,最后,最大流对300取余就是边数啦.. #incl ...

  10. 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 ...

随机推荐

  1. Bubble Sort(冒泡排序)

    冒泡排序(英语:Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行 ...

  2. Python模块logging

    基本用法: import logging import sys # 获取logger实例,如果参数为空则返回root logger logger = logging.getLogger("A ...

  3. Java8---函数式编程-示例

    // Java8函数式编程示例—(Predicate.Stream.Optional) https://blog.csdn.net/weixin_41950473/article/details/84 ...

  4. C++中对象的构造顺序

    1,C++ 中的类可以定义多个对象,那么对象构造顺序是怎样的? 1,很多的 bug 是由对象的构造顺序造成的,虽然它不难: 2,对象的构造往往和构造函数牵涉在一起,构造函数的函数体又可能由非常复杂的程 ...

  5. C++自学教程第一课——你好世界,我是柠檬鲸。

    C++系列教程现在在自己学校的一个博客平台发布,几个朋友一起搭建的 [C++基础教程系列](https://blog.ytmaxoj.org/cpp_basic_liuary-0/) 下面是原来的正文 ...

  6. 虚拟机的网卡基本配置和基本linux命令

    1.切换到/etc/sysconfig/network-script目录 cd /etc/sysconfig/network-scripts 2.将ifcfg-eth0备份成ifcfg-eth0. c ...

  7. Java Annotation 刷课笔记(一)

    1.什么是注解? 1.1.Annotation是从JDK5.0引入的新技术 1.2.Annotation的作用: 不是程序本身,可以对程序作出解释(这一点,和注释没什么区别) 可以被其他程序(比如编译 ...

  8. jenkins插件send files or execute commands over ssh插件parameterized publishing选项使用

    1.设置一个参数 2.设置label 3.勾选parameterized publishing

  9. linear_func

    ''' class torch.nn.Linear(in_features,out_features,bias = True )[来源] 参数: in_features - 每个输入样本的大小out_ ...

  10. dd命令注意:dd:unrecognized operand 'if'

    如果是 idd if=boot.bin 在等号两边不要有空格