Minimum Cut
Time Limit:2000MS Memory Limit:102400KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
Given a simple unweighted graph
G
(an undirected graph containing no loops nor multiple edges) with
We say that a cut in
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
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
The following
n−1
lines describe the spanning tree
T
and each of them contains two integers
Next
Output
For each test case, you should output the minimum cut of graph
G
respecting the given spanning tree
Sample Input
1
4 5
1 2
2 3
3 4
1 3
1 4
Sample Output
Case #1: 2
分析:
G是一个图,T是他的生成树。问至少去掉多少天边可以使这个图不连通,却只能去掉生成树中的一条边。
由于只能去掉生成树中的一条边,所以一定是去掉在生成树中度为1的边(入度+出度),并且在整个图中度最少的节点,使之不连通。td[i]表示j节点i在生成树中的度,gd[i]表示节点i在生成树之外的度。所以结果就是td[i]==1,且min(gd[i]).
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 20010;
int td[maxn],gd[maxn];
int main()
{
int t,n,m,a,b,cas=0;
scanf("%d",&t);
while(t--)
{
memset(td,0,sizeof(td));
memset(gd,0,sizeof(gd));
cas++;
scanf("%d%d",&n,&m);
for(int i=0;i<n-1;i++)
{
scanf("%d%d",&a,&b);
td[a]++;td[b]++;
}
for(int i=0;i<m-n+1;i++)
{
scanf("%d%d",&a,&b);
gd[a]++;gd[b]++;
}
int ans = 20010;
for(int i=1;i<=n;i++)
{
if(td[i]==1)
{
ans=min(ans,td[i]+gd[i]);
}
}
printf("Case #%d: %d\n",cas,ans);
}
return 0;
}