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

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

2015 ACM/ICPC Asia Regional Shenyang Online

题意:一无权无向无环无多余边图G,由n个结点,m条边构成。T是其生成树。如果T除去一条边,G有一个割可以代表T,我们这么说。找最小割

输入n, m;前 n -1行代表T,之后m-n+1行是G中T没有的边。求最小割。

割就是,在T中删除一条边,在G中需要删除几条边可以变成T的图的连接关系。在m-n-1行中都是T中没有的边,添加了(u, v)之后,v中的子节点,叶子节点都可以和u联系,那么在求最小割的时候就要删除这条边,使得v中原本不能与u联系的还是不能联系变成 T 图的关系。

用cnt存该点由G变成T(在T中删除一条边)变 了 n-1次(一次删除一条边)之后,需要被删除的次数。遍历求最小值即最小割

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 #include <stack>
 9 #include <cstring>
10
11 using namespace std;
12
13 #define INF 0x3f3f3f3f
14 #define min(a,b) (a<b?a:b)
15 #define N 100005
16
17 vector< vector<int> > G;
18
19 int n, m, cnt[N], dfn[N], f[N];
20
21 void init()
22 {
23     G.resize(N);
24     G.clear();
25     memset(dfn, 0, sizeof(dfn));
26 }
27
28 void Tarjan(int u, int fa, int step)
29 {
30     dfn[u] = step;
31     cnt[u] = 1;
32     f[u] = fa;
33     int len = G[u].size();
34     for(int i = 0; i < len; i++)
35     {
36         int v = G[u][i];
37         if(v != fa)
38             Tarjan(v, u, step+1);
39     }
40 }
41
42 void Lca(int x, int y)
43 {
44     while(x != y)
45     {
46         if(dfn[x] >= dfn[y])   //dfn表示树的深度,找深的父节点
47         {
48             cnt[x]++;
49             x = f[x];
50         }
51         else
52         {
53             cnt[y]++;
54             y = f[y];
55         }
56     }
57 }
58
59 int main()
60 {
61     int t, i, a, b, l = 1;
62     scanf("%d", &t);
63     while(t--)
64     {
65         init();
66         scanf("%d%d", &n, &m);
67         for(i = 1; i < n; i++)
68         {
69             scanf("%d%d", &a, &b);
70             G[a].push_back(b);
71             G[b].push_back(a);
72         }
73         Tarjan(1, 0, 1);
74         for(; i <= m; i++)
75         {
76             scanf("%d%d", &a, &b);
77             Lca(a, b);
78         }
79         int ans = INF;
80         for(i = 2; i <= n; i++)
81             ans = min(ans, cnt[i]);
82         printf("Case #%d: %d\n", l++ ,ans);
83     }
84     return 0;
85 }
时间: 2024-07-30 23:24:18

Minimum Cut的相关文章

HDU 5452 Minimum Cut

Minimum Cut Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 315    Accepted Submission(s): 120 Problem Description Given a simple unweighted graph G (an undirected graph containing no loops nor

POJ2914 Minimum Cut 最小割集

题目大意是,给定N个顶点,M条边,两个顶点之间可能有多条边,求至少删除多少条边才能将该图分成两个子图. 最小割集,典型的算法Stoer-Wagner,就是那篇论文,这里也就不复制过来了,只是用Prim求最大生成树时,更新的"边"不是普通意义上的边,而是顶点到所有已划分集合中的所有点的边权值和,这里要特别注意~ 直接贴代码~ #include <stdio.h> #include <vector> #include <math.h> #include

hdu 5452 Minimum Cut 树形dp

Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5452 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 spa

POJ 2914 Minimum Cut 最小割图论

Description Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must be removed at least to disconnect the graph into two subgraphs? Input Input co

hdu 6214 Smallest Minimum Cut[最大流]

hdu 6214 Smallest Minimum Cut[最大流] 题意:求最小割中最少的边数. 题解:对边权乘个比边大点的数比如300,再加1 ,最后,最大流对300取余就是边数啦.. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<vector> 7 #in

2015沈阳网络赛1003 Minimum Cut 树链剖分 数组维护前缀和进行区间增减

2015沈阳网络赛1003  Minimum Cut   树链剖分 数组维护前缀和进行区间增减 Minimum Cut Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Given a simple unweighted graph G 

HDU 5452——Minimum Cut——————【树链剖分+差分前缀和】

Minimum Cut Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total Submission(s): 895    Accepted Submission(s): 387 Problem Description Given a simple unweighted graph G (an undirected graph containing no loops nor

POJ2914 Minimum Cut 【全局最小割】(Stoer_Wagner)

Minimum Cut Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 7610   Accepted: 3203 Case Time Limit: 5000MS Description Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum c

Hdu 5452 Minimum Cut (2015 ACM/ICPC Asia Regional Shenyang Online) dfs + LCA

题目链接: Hdu 5452 Minimum Cut 题目描述: 有一棵生成树,有n个点,给出m-n+1条边,截断一条生成树上的边后,再截断至少多少条边才能使图不连通, 问截断总边数? 解题思路: 因为只能在生成树上截断一条边(u, v),所以只需要统计以v为根节点的子生成树里的节点与子生成树外的节点的边数就可以了.对于新加入的边(u', v')来说,只影响以LCA(u, v)为根节点的子树里面的节点.统计所有答案,扫一遍输出最小即可.(比赛的时候只统计叶子节点,给水过去了........233