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 ...
随机推荐
- ajax 几种提交方式
方式一: $.ajax({ type: 'POST', url: "/user/editPwd.htm", data: {"oldPassword":oldPa ...
- Android 程序中得到root activity的引用
今天写anroid时,想获得一个root activity的引用. ios中这个很简单, [UIApplication sharedApplication].keyWindow 得到window对象, ...
- Windows下配置Java开发环境
学习Java第一步是配置本地开发环境,学习最基本的桌面开发,下面以win7为例配置Java开发环境,即:JDK+JRE+Eclipse,安装JDK的时候会默认安装JRE,根据提示安装就可以了. 首先去 ...
- MongoDB 副本集管理(不定时更新)
简介: 前面介绍完了副本集的搭建.用户的管理.参数和日常操作的说明,那副本集搭建好该如何管理呢?现在来说明下副本集的日常查看和管理. 说明: 1)查看命令行参数:db.serverCmdLineOpt ...
- [转]Android 学习资料分享(2015 版)
转 Android 学习资料分享(2015 版) 原文地址:http://www.jianshu.com/p/874ff12a4c01 目录[-] 我是如何自学Android,资料分享(2015 版) ...
- 让复杂Json数据和对象自由转换 --- Gson
Gson是谷歌用于对Json操作的库,里面有着强大而又方便的功能,最常用的就是 fromJson():将json数据转化为对象: toJson():将对象转化为json数据! 对于普通的json数据使 ...
- 【vs2010调试】当前不会命中断点 源代码与原始版本不同
解决方案:全选CPP文件内容,选择 “编辑”-“高级”-“设置选定内容的格式”,保存,重新编译.
- 【EM】代码理解
本来想自己写一个EM算法的,但是操作没两步就进行不下去了.对那些数学公式着实不懂.只好从网上找找代码,看看别人是怎么做的. 代码:来自http://blog.sina.com.cn/s/blog_98 ...
- 【linux】虚拟机安装centos后ping ip地址出现错误:Network is unreachable
来源:https://my.oschina.net/stonezing/blog/515480 方案一: 进入/etc/sysconfig/network-scripts/ 查看这下面的文件 每个人的 ...
- java课后作业 弹出窗口求两个数的加减乘除
//计算2个数的加减乘除 谷伟华 2015/10/6package jisuan; import javax.swing.JOptionPane; public class Jiasuan { pub ...