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 ...
随机推荐
- 大小端; union
#include<stdio.h> #include <stdlib.h> typedef union { int m; char a[4]; }Node; int main ...
- CentOS 7部署flume
CentOS 7部署flume 准备工作: 安装java并设置java环境变量,在`/etc/profile`中加入 export JAVA_HOME=/usr/java/jdk1.8.0_65 ex ...
- 【转】mysql的cardinality异常,导致索引不可用
转自:http://ourmysql.com/archives/1343 前段时间,一大早上,就收到报警,警告php-fpm进程的数量超过阈值.最终发现是一条sql没用到索引,导致执行数据库查询慢了, ...
- docker -v挂载数据卷网络异常的问题
docker 删除容器并重新运行容器时报如下异常: docker: Error response from daemon: failed to create endpoint tomcat001 on ...
- Java中的Comparable接口和Comparator接口
Comparator位于包java.util下,比较器,是在集合外部定义排序.Comparable位于包java.lang下,代表当前对象可比较的,是在集合内部实现排序. Comparable代表一个 ...
- 【hihoCoder】第20周 线段树
题目: 输入 每个测试点(输入文件)有且仅有一组测试数据. 每组测试数据的第1行为一个整数N,意义如前文所述. 每组测试数据的第2行为N个整数,分别描述每种商品的重量,其中第i个整数表示标号为i的商品 ...
- POJ 2965 The Pilots Brothers' refrigerator
题目链接 题意:一个冰箱上有4*4共16个开关,改变任意一个开关的状态(即开变成关,关变成开)时,此开关的同一行.同一列所有的开关都会自动改变状态.要想打开冰箱,要所有开关全部打开才行. 输入:一个4 ...
- ServletConfig与ServletContext
ServletConfig与ServletContext对象详解 一.ServletConfig对象 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...
- [Android Pro] Android下toolbox简介
toolbox是Android 自带的提供shell命令的软件.有点类似于busybox,但功能上好像弱很多.其源码可以从Android source code 中system/core/toolbo ...
- eclipse maven新建springMVC项目(原创)
1.配置eclipse maven 2.新建maven项目 3.新建src/main/java,更新pom <project xmlns="http://maven.apache.or ...