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

HINT

题意

给你一个没有自环重边的无向图,然后给你一颗生成树,让你求一个最小割集的大小,使得这个割集有且只有一条边在这棵生成树上

题解:

树形dp,对于这个点,如果要消除他和他父亲之间的联系的话,代价就是他的子树所有连向外界的边就好了

跑一遍dfs维护一下就行了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 20050
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
//*********************************************************************************

vector<int> Q[maxn];
vector<int> E[maxn];
int vis[maxn];
int dp[maxn];
int ans;
void dfs(int x)
{
    vis[x]=1;
    for(int i=0;i<Q[x].size();i++)
    {
        int y=Q[x][i];
        dfs(Q[x][i]);
        dp[x]+=dp[y]-1;
    }
    ans = min(ans,dp[x]);
    for(int i=0;i<E[x].size();i++)
        if(vis[E[x][i]])
            dp[E[x][i]]--;
    vis[x]=0;
}
int main()
{
    int t;scanf("%d",&t);
    for(int cas = 1;cas<=t;cas++)
    {
        ans = 99999999;
        int n,m;scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
            Q[i].clear(),E[i].clear();
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n-1;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            dp[x]++,dp[y]++;
            if(x>y)swap(x,y);
            Q[x].push_back(y);
        }
        for(int i=n-1;i<m;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            dp[x]++,dp[y]++;
            if(x>y)swap(x,y);
            E[y].push_back(x);
        }
        dfs(1);
        printf("Case #%d: %d\n",cas,ans);
    }
}
时间: 2024-08-08 01:10:13

hdu 5452 Minimum Cut 树形dp的相关文章

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

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

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

HDU 1011 Starship Troopers(树形DP)

Starship Troopers Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 62   Accepted Submission(s) : 12 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description You, the leader of

hdu 1520Anniversary party(简单树形dp)

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4310    Accepted Submission(s): 1976 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe

HDU 2196 Computer 经典树形DP

一开始看错题了,后来发现原来是在一颗带权的树上面求出距离每一个点的最长距离,做两次dfs就好,具体的看注释? #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #incl

HDU 3586.Information Disturbing 树形dp

Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 3205    Accepted Submission(s): 1137 Problem Description In the battlefield , an effective way to defeat enemies is to bre

HDU 2376 Average distance (树形dp)

Average distance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 588    Accepted Submission(s): 213 Special Judge Problem Description Given a tree, calculate the average distance between two ve