题意:切掉树上的某条边,使分开的两棵树上各点的权值和差值最小。

与hdu2196不同的是,此题是点权,其他无太大差别,注意数据范围。

先求出每个节点的子树权值和,然后自底向上dp即可。取$\min (abs(sum - 2*dp[u]))$

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
const ll inf=1e17+;
ll head[maxn],tot,n,m,sum;
struct edge{
ll to;
ll nxt;
ll w;
}e[maxn<<];
void add_edge(int u,int v,int w){
e[tot].to=v;
e[tot].w=w;
e[tot].nxt=head[u];
head[u]=tot++;
} ll dp[maxn<<],cnt[maxn<<]; void dfs(ll u,ll fa){
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
if(v==fa) continue;
dfs(v,u);
cnt[u]+=1ll*cnt[v];
}
dp[u]=min(dp[u],sum-*cnt[u]>=?sum-*cnt[u]:*cnt[u]-sum);
} inline int read(){
char k=;char ls;ls=getchar();for(;ls<''||ls>'';k=ls,ls=getchar());
int x=;for(;ls>=''&&ls<='';ls=getchar())x=(x<<)+(x<<)+ls-'';
if(k=='-')x=-x;return x;
} int main(){
int k=;
while(scanf("%lld%lld",&n,&m)!=EOF&&(n||m)){
fill(dp+,dp+n+,inf);
memset(head,-,sizeof head);
tot=;
sum=;
int a,b;
for(int i=;i<=n;i++){
cnt[i]=read();
sum+=1ll*cnt[i];
}
for(int i=;i<m;i++){
a=read();
b=read();
add_edge(a,b,);
add_edge(b,a,);
}
printf("Case %d: ",++k);
dfs(,-);
ll ans=inf;
for(int i=;i<=n;i++){
ans=min(ans,dp[i]);
}
printf("%lld\n",ans);
}
return ;
}

[poj3140]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. Hackrank Kingdom Division 树形DP

    题目链接:传送门 题意: 给你一棵树,n个点 每个点可以染成红色和蓝色 但是红色的点与其相邻的点中必须有红色节点,蓝色也是 问你有多少种染色的方案 题解: 树形dp 先转化为有根树,取1为根 设定dp ...

  3. POJ 3140 Contestants Division (树dp)

    题目链接:http://poj.org/problem?id=3140 题意: 给你一棵树,问你删去一条边,形成的两棵子树的节点权值之差最小是多少. 思路: dfs #include <iost ...

  4. POJ-3140 Contestants Division (树)

    题目大意:一棵树,带点权.将这棵树分成两部分,找出使得两部分的点权和的差最小. 题目分析:直接dfs即可.找出每棵子树u的点权和size(u),如果以u和它的父节点之间的边为界,那么两边的点权和分别为 ...

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

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

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

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

  7. POJ 3140 Contestants Division 【树形DP】

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

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

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

  9. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

随机推荐

  1. 在mac下搭建java开发环境

    刚刚从windows系统转到使用mac系统.感觉不是特别熟悉,须要一定的适应时间. 以下简介一下mac下搭建主要的java开发环境. 1.安装jdk 安装jdk1.7后,发现不须要进行环境变量配置,直 ...

  2. Drcom账户管理Server端解说

    https://www.github.com/xiyouMc 首先今天要讲的是针对Drcom查询账户URL的解析和抓取数据.    Drcom是大学生宿舍上网普遍使用的联网client,然而对于自己账 ...

  3. python 基础 7.0 import 导入

    一. python 常用内置模块的使用(datetime,logging,os,command)       在日常的开发工作中,我们要写很多的python 代码,如果都写在一个文件中,会导致代码特别 ...

  4. 两个DataGridEHToExcel

    procedure TForm1.N1Click(Sender: TObject); var    GridtoExcel: TDBGridEhToExcel; begin    try    Gri ...

  5. 宇视摄像机RTSP地址格式规则

    rtsp://{用户名}:{密码}@{ip}:{port}/video1/2/3,分别对应主/辅/三码流: 比如: rtsp://admin:admin@192.168.8.8:554/video1, ...

  6. 九度OJ 1075:斐波那契数列 (数字特性)

    时间限制:5 秒 内存限制:32 兆 特殊判题:否 提交:3121 解决:1806 题目描述: 编写一个求斐波那契数列的递归函数,输入n值,使用该递归函数,输出如样例输出的斐波那契数列. 输入: 一个 ...

  7. 九度OJ 1058:反序输出 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8454 解决:3042 题目描述: 输入任意4个字符(如:abcd), 并按反序输出(如:dcba) 输入: 题目可能包含多组用例,每组用例 ...

  8. 限制线程数 Limit the number of threads started by colly 随机延迟

    random delay | Colly http://go-colly.org/docs/examples/random_delay/

  9. drawable animation基础

    动画配置<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android: ...

  10. Machine Learning No.2: Linear Regression with Multiple Variables

    1. notation: n = number of features x(i) = input (features) of ith training example  = value of feat ...