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
Sample Output
HINT
题意
给你一个没有自环重边的无向图,然后给你一颗生成树,让你求一个最小割集的大小,使得这个割集有且只有一条边在这棵生成树上
题解:
树形dp,对于这个点,如果要消除他和他父亲之间的联系的话,代价就是他的子树所有连向外界的边就好了
跑一遍dfs维护一下就行了
-------------------------
我的方法是错的,已经被hack了
数据:
4 4 1 2 2 3 3 4 1 3
4 4 1 3 2 3 2 4 1 2
代码:
//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[];
//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]=;
for(int i=;i<Q[x].size();i++)
{
int y=Q[x][i];
dfs(Q[x][i]);
dp[x]+=dp[y]-;
}
ans = min(ans,dp[x]);
for(int i=;i<E[x].size();i++)
if(vis[E[x][i]])
dp[E[x][i]]--;
vis[x]=;
}
int main()
{
int t;scanf("%d",&t);
for(int cas = ;cas<=t;cas++)
{
ans = ;
int n,m;scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
Q[i].clear(),E[i].clear();
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
for(int i=;i<n-;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-;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();
printf("Case #%d: %d\n",cas,ans);
}
}
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条边,截断一条生成树上的边后,再截断至少多少条边才能使图不连通, 问截断总边数? 解题思路: 因 ...
- HDU 5452——Minimum Cut——————【树链剖分+差分前缀和】ACdream 1429——Diversion——————【树链剖分】
Minimum Cut Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)Tota ...
- HDU 5452 Minimum Cut(LCA)
http://acm.hdu.edu.cn/showproblem.php?pid=5452 题意: 有一个连通的图G,先给出图中的一棵生成树,然后接着给出图中剩余的边,现在要删除最少的边使得G不连通 ...
- HDU 5452 Minimum Cut
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5452题目大意: 给你一个图G,图中包含一颗生成树.要求只能删除生成树内的一条边,使得图不联通.问最小的删除 ...
- HDU 5452 Minimum Cut (Spaning Tree)
生成树的上的一个非根结点对应一条生成树上的边,然后这个结点的子树上连出去的边就对应去掉这条边的割, 然后就可以对树外的边求LCA,在LCA上标记,利用这个信息可以算出有多少条边在子树上,以及有多少条边 ...
- HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价
Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/ ...
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- hdu 1520Anniversary party(简单树形dp)
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- R12 - OM改进了对成本与收入确认的流程
我们知道在企业经营活动中,根据财务制度的要求,对于收入与成本确认有很复杂的原则,这里就不去细讨论这些原则了,要了解的话可以看纵横四海的BLOG: 中也有,但11中是灰的. 这个科目什么时候发挥作用呢? ...
- Android开发之bug-No Activity found to handle Intent
android.content.ActivityNotFoundException: No Activity found to handle Intent 做Android开发中,使用隐式intent ...
- Maven相关内容学习笔记一:基本配置和使用
首先必须推荐的这本书<Maven实战> 许晓斌,机械工业出版社 Maven简介 其实使用Maven也有很久时间了,大部分都是别人建好了工程我使用一下,实际上并没有非常详细的使用经验,这次到 ...
- poj3671
首先容易想到的是LIS,但是n<=30000,所以肯定要优化: 壮哉单调队列又登场了: 然后再找一个最长不上升序列并求两者最大值即可,复杂度O(n logn); 应该说这是解题通法了,但再回头看 ...
- POJ 3377 Ferry Lanes
虽然它出现在dp专场里···但是我第一反应是一道最短路题···不过幸好它出现在dp专场里···因为我不怎么会dijstra什么的··· 题意:一条河上有N+1对码头,每个相邻码头之间需要一定时间到达, ...
- (Android Studio)添加文本框
此文大部分摘自http://hukai.me/android-training-course-in-chinese/basics/firstapp/building-ui.html android : ...
- 28、activity之间传递数据&批量传递数据
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...
- HDU 3533 Escape BFS搜索
题意:懒得说了 分析:开个no[100][100][1000]的bool类型的数组就行了,没啥可说的 #include <iostream> #include <cstdio> ...
- [转载]C++命名规则
在软件开发这一高度抽象而且十分复杂的活动中,命名规则的重要性更显得尤为突出.一套定义良好并且完整的.在整个项目中统一使用的命名规范将大大提升源代码的可读性和软件的可维护性. 在引入细节之前,先说明一下 ...
- A Tour of Go Switch evaluation order
Switch cases evaluate cases from top to bottom, stopping when a case succeeds. (For example, switch ...