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 ...
随机推荐
- poj3592Instantaneous Transference(tarjan+spfa)
http://poj.org/problem?id=3592提交了30多次了 受不了了 两份的代码基本上一样了 一个AC一个WA 木办法 贴份别人的吧 改得跟我得一样 人家能A 我是WA.. 强连通 ...
- 在QuickReport中实现多栏打印
如果在Treport的DataSet属性中选定一个Table,那么QuickReport每次打印详细列表(BandType=rbDetail)属性的TQRBand时,系统会自动取出一个记录供打印, ...
- android studio 安装总结
Android Studio 的安装和配置篇(Windows篇<转> http://www.jianshu.com/p/fc03942548cc# 中间gradle下载比较慢:解决方法 需 ...
- android——创建camera应用(译)
只是选择相机部分来翻译.下面是主要内容 有些开发者可能需要Camera的接口,来定制自己程序的外观和特殊功能.创建自定义的Camera界面比使用using an Intent需要编写更多的代码,但是 ...
- sql 统计用的sql
mh:工时 mhtype:工时类型(6种) 字段:userid mhtype mh 001 1 5 001 ...
- Convert to Objective-C ARC
今天在进行代码走查时,竟然发现了下面这段代码: Bad Code 顿时感觉吐槽无力,虽然我反复强调内存管理问题,无非就是谁申请谁释放,利用强弱引用避免 retain-cycles,但是还是会有这样那样 ...
- 多线程之RunLoop
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- windows 下Python import 导入自定义模块
周末在家研究这个东西,则找到解决方案. 费话少说,上代码 #定义一个自定义的函数,如下 #函数的名称必须是字母和数字的组合,不能用数字开头 #函数名后用小括号括住入参,可以用逗号分隔多个 #如果有返回 ...
- Solaris10下Telnet、SSH、ftp使用root登录
学习solaris过程中,使用SecureCRT.putty等工具用root账号登录,总是登录不上去,后来经老师解答和网上找答案,发现是 solaris默认是不可以远程使用root账号登录的,如果想开 ...
- C程序内存分配
在多任务操作系统中的每一个进程都运行在一个属于它自己的内存沙盘中.这个沙盘就是虚拟地址空间(virtual address space),在32位模式下它总是一个4GB的内存地址块.这些虚拟地址通过页 ...