POJ 3140 Contestants Division 树形DP
Description
In the new ACM-ICPC Regional Contest, a special monitoring and submitting system will be set up, and students will be able to compete at their own universities. However there’s one problem. Due to the high cost of the new judging system, the organizing committee can only afford to set the system up such that there will be only one way to transfer information from one university to another without passing the same university twice. The contestants will be divided into two connected regions, and the difference between the total numbers of students from two regions should be minimized. Can you help the juries to find the minimum difference?
Input
There are multiple test cases in the input file. Each test case starts with two integers N and M, (1 ≤ N ≤ 100000, 1 ≤ M ≤ 1000000), the number of universities and the number of direct communication line set up by the committee, respectively. Universities are numbered from 1 to N. The next line has N integers, the Kth integer is equal to the number of students in university numbered K. The number of students in any university does not exceed 100000000. Each of the following M lines has two integers s, t, and describes a communication line connecting university s and university t. All communication lines of this new system are bidirectional.
N = 0, M = 0 indicates the end of input and should not be processed by your program.
Output
For every test case, output one integer, the minimum absolute difference of students between two regions in the format as indicated in the sample output.
Sample Input
7 6
1 1 1 1 1 1 1
1 2
2 7
3 7
4 6
6 2
5 7
0 0
Sample Output
Case 1: 1
题意:
给你一个n点的树,
每个点有权值,现在让你删除一条边,使得剩下的两个子树的权值和差值最小,并输出
题解;
我们以1为根做一遍DP
求出dp[i] 以i为根节点的子树权值总和
再枚举删除哪一条边即可
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e5+, M = 1e2+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int n,m,a[N],b[N];
ll v[N],dp[N];
vector < int > G[N];
void dfs(int u,int fa) {
dp[u] = v[u];
for(int i=;i<G[u].size();i++) {
int to = G[u][i];
if(to == fa) continue;
dfs(to,u);
dp[u] += dp[to];
}
}
int main()
{
int cas = ;
while(~scanf("%d%d",&n,&m)) {
if(!n&&!m) break;
memset(dp,,sizeof(dp));
ll all = ;
for(int i=;i<=n;i++) scanf("%I64d",&v[i]),all+=v[i];
for(int i=;i<=n;i++) G[i].clear(); for(int i=;i<=m;i++) {
scanf("%d%d",&a[i],&b[i]);
G[a[i]].push_back(b[i]);G[b[i]].push_back(a[i]);
} dfs(,-);
ll ans = 1e18;
for(int i=;i<=m;i++)
if(all - min(dp[b[i]],dp[a[i]]) - min(dp[b[i]],dp[a[i]]) < )
ans = min(ans, - all + min(dp[b[i]],dp[a[i]]) + min(dp[b[i]],dp[a[i]]));
else ans = min(ans, all - min(dp[b[i]],dp[a[i]]) - min(dp[b[i]],dp[a[i]])); printf("Case %d: %I64d\n",cas++,ans);
}
}
POJ 3140 Contestants Division 树形DP的更多相关文章
- POJ 3140 Contestants Division (树dp)
题目链接:http://poj.org/problem?id=3140 题意: 给你一棵树,问你删去一条边,形成的两棵子树的节点权值之差最小是多少. 思路: dfs #include <iost ...
- POJ 3140.Contestants Division 基础树形dp
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10704 Accepted: ...
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- POJ 3140 Contestants Division 【树形DP】
<题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...
- POJ 3140 Contestants Division (树形DP,简单)
题意: 有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小.问差的绝对值.(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1) 思路: ...
- POJ 3140 Contestants Division
题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...
- poj 3140 Contestants Division [DFS]
题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小. 思路:这个题我直接dfs做的,不知道树状dp是什么思路..一开始看到数据规模有些后怕,后来想到long long 可以达 ...
- [poj3140]Contestants Division树形dp
题意:切掉树上的某条边,使分开的两棵树上各点的权值和差值最小. 与hdu2196不同的是,此题是点权,其他无太大差别,注意数据范围. 先求出每个节点的子树权值和,然后自底向上dp即可.取$\min ( ...
- POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)
POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...
随机推荐
- mach 和 array 方法
- 多线程同步_Monitor
多线程一直在学习和理解中...... Monitor类是多线程中用以实现同步的一种技术,主要是同一进程内多线程间的同步技术. Monitor类中有以下几个方法需要注意: Monitor.Enter(o ...
- reportng的使用
1.首先安装testng 2.下载reportng jar包 http://pan.baidu.com/s/1i3KdlQH 3.添加到project build path 注意:需要同时引入goog ...
- Innodb锁机制:Next-Key Lock 浅谈
数据库使用锁是为了支持更好的并发,提供数据的完整性和一致性.InnoDB是一个支持行锁的存储引擎,锁的类型有:共享锁(S).排他锁(X).意向共享(IS).意向排他(IX).为了提供更好的并发,Inn ...
- modelsim do文件仿真
1.编写sim.do文件 ##### Quit the Simulation ##### quit -sim ##### Make work directory && Create ...
- Silverlight 动画详解
Animation规则 基于时间:你设置动画的初始状态,最终状态,及持续时间,Silverlight会计算帧速率. 作用于属性(properties):一个Silverlight动画只能做一件事情,在 ...
- 【XLL API 函数】 xlfSetName
常常用于创建和删除与DLL定义的名称 原型 Excel12(xlfSetName, LPXLOPER12 pxRes, 2, LPXLOPER12 pxNameText, LPXLOPER12 pxN ...
- 解决eclipse中egit中的cannot open git-upload-pack问题
一.背景 今天在使用eclipse的egit插件进行检出远程代码到本地时,出现了cannot open git-upload-pack错误,后经过努力解决该问题,记录下方便回顾和交流! 二.出现原因 ...
- Mysql之日志恢复
对于Mysql,每一步操作都会有相应记录,如insert,update,delete ,drop ,alter等相关DDL或DML操作.有时难免会出错,但在出错时如何恢复以复原数据. 例如,现在有这些 ...
- MysqlDumpslow
可以帮助分析慢查询. 选项: -n 10 列出最近10条慢查询 如: mysqldumpslow