题目链接:http://poj.org/problem?id=3140

题意:

给你一棵树,问你删去一条边,形成的两棵子树的节点权值之差最小是多少。

思路:

dfs

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1e5 + ;
typedef long long LL;
int n, cnt, head[N];
LL a[N];
struct Edge {
int next, to;
}edge[N << ];
LL dp[N]; inline void add(int u, int v) {
edge[cnt].next = head[u];
edge[cnt].to = v;
head[u] = cnt++;
} LL dfs(int u, int p) {
dp[u] = a[u];
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
dp[u] += dfs(v, u);
}
return dp[u];
} LL f(LL a) {
return a > ? a : -a;
} int main()
{
int m, ca = ;
while(~scanf("%d %d", &n, &m) && (n || m)) {
LL sum = ;
for(int i = ; i <= n; ++i) {
scanf("%lld", a + i);
sum += a[i];
head[i] = -;
}
cnt = ;
int u, v;
while(m--) {
scanf("%d %d", &u, &v);
add(u, v);
add(v, u);
}
dfs(, -);
LL res = sum;
for(int i = ; i <= n; ++i) {
res = min(res, f(sum - *dp[i]));
}
printf("Case %d: %lld\n", ca++, res);
}
return ;
}

POJ 3140 Contestants Division (树dp)的更多相关文章

  1. POJ 3140 Contestants Division 树形DP

    Contestants Division   Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...

  2. POJ 3140.Contestants Division 基础树形dp

    Contestants Division Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10704   Accepted:  ...

  3. poj 3140 Contestants Division(树形dp? dfs计数+枚举)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  4. POJ 3140 Contestants Division 【树形DP】

    <题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...

  5. POJ 3140 Contestants Division (树形DP,简单)

    题意: 有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小.问差的绝对值.(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1) 思路: ...

  6. POJ 3140 Contestants Division

    题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...

  7. poj 3140 Contestants Division [DFS]

    题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小. 思路:这个题我直接dfs做的,不知道树状dp是什么思路..一开始看到数据规模有些后怕,后来想到long long 可以达 ...

  8. POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)

    POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...

  9. POJ 3104 Contestants Division

    Contestants Division Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10597   Accepted:  ...

随机推荐

  1. Applied Nonparametric Statistics-lec10

    Ref:https://onlinecourses.science.psu.edu/stat464/print/book/export/html/14 估计CDF The Empirical CDF ...

  2. poj 3176 三角数和最大问题 dp算法

    题意:给一个三角形形状的数字,从上到下,要求数字和最大 思路 :dp dp[i+1][j]=max(dp[i+1][j],dp[i][j]+score[i+1][j]) dp[i+1][j+1]=ma ...

  3. optimize table在优化mysql时很重要

    一个表的数据量有1000W条,那么查看这么表占据的硬盘空间时会发现,数据本身是300M,索引是200M 这个时候,删除掉500W条数据,这个时候数据本身150M,而索引还是200M左右 你删除数据时, ...

  4. mysql查询的语法

    单表查询语法 SELECT DISTINCT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条 ...

  5. 反转单词顺序 VS 左旋转字符串

    题目一:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标垫符号和普通字母一样处理.例如输入字符串“I am a student.”,则输出“student. a am I ...

  6. Python 前端 js基础

    Javascript 概述 JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的 ...

  7. 大数据学习——scala类相关操作

    1 类的定义 package com /** * Created by Administrator on 2019/6/3. */ //类并不用声明为public. class Person { // ...

  8. matlab 画图进阶

    matlab 画图进阶 applications of matlab in engineering 图表类型的选择 first:advanced 2d plots special plots logl ...

  9. session的工作原理、django的超时时间设置及session过期判断

    1.session原理 cookie是保存在用户浏览器端的键值对 session是保存在服务器端的键值对 session服务端中存在的数据为: session = { 随机字符串1:{ 用户1的相关信 ...

  10. hnust 不爱学习的小w

    问题 C: 不爱学习的小W 时间限制: 2 Sec  内存限制: 64 MB提交: 1431  解决: 102[提交][状态][讨论版] 题目描述 “叮铃铃”上课了,同学们都及时到了教室坐到了座位上, ...