poj3140Contestants Division
这叫树形DP吗。。?断开某条边 求剩下两颗树数权值和的差最小
dfs一遍 枚举边 查了n久 wa n次 dp数组没初始化。。
在poj上1A感觉应该挺爽
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define N 100010
#define M 1000010
#define LL long long
LL dp[M<<],p[N];
struct node
{
int u,v,next;
}ed[M<<];
int head[N],t,n,m;
void init()
{
t = ;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
ed[t].u = u;
ed[t].v = v;
ed[t].next = head[u];
head[u] = t++;
}
LL dfs(int pre,int u,int e)
{
int i;
LL sum=p[u];
for(i = head[u] ; i != - ; i = ed[i].next)
{
int v = ed[i].v;
if(v==pre)
continue;
sum+=dfs(u,v,i);
}
dp[e] = sum;
return dp[e];
}
int main()
{
int i,kk=;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==)
break;
init();kk++;
memset(dp,,sizeof(dp));
LL s=;
for(i =; i <= n; i++)
{
scanf("%lld",&p[i]);
s+=p[i];
}
for(i = ; i <= m ;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(-,i,);
LL ans = s,o;
for(i = ; i < t ; i++)
{
if(s-dp[i]>dp[i])
o = s-dp[i]-dp[i];
else
o = dp[i]-(s-dp[i]);
if(o<ans)
ans = o;
}
printf("Case %d: ",kk);
printf("%lld\n",ans);
}
return ;
}
poj3140Contestants Division的更多相关文章
- python from __future__ import division
1.在python2 中导入未来的支持的语言特征中division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/&qu ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- 关于分工的思考 (Thoughts on Division of Labor)
Did you ever have the feeling that adding people doesn't help in software development? Did you ever ...
- POJ 3140 Contestants Division 树形DP
Contestants Division Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...
- 暴力枚举 UVA 725 Division
题目传送门 /* 暴力:对于每一个数都判断,是否数字全都使用过一遍 */ #include <cstdio> #include <iostream> #include < ...
- GDC2016【全境封锁(Tom Clancy's The Division)】对为何对应Eye Tracked System,以及各种优点的演讲报告
GDC2016[全境封锁(Tom Clancy's The Division)]对为何对应Eye Tracked System,以及各种优点的演讲报告 原文 4Gamer編集部:松本隆一 http:/ ...
- Leetcode: Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- hdu 1034 (preprocess optimization, property of division to avoid if, decreasing order process) 分类: hdoj 2015-06-16 13:32 39人阅读 评论(0) 收藏
IMO, version 1 better than version 2, version 2 better than version 3. make some preprocess to make ...
- uva 725 Division(暴力模拟)
Division 紫书入门级别的暴力,可我还是写了好长时间 = = [题目链接]uva 725 [题目类型]化简暴力 &题解: 首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数 ...
随机推荐
- javascript 第27节 jQuery选择器
下面的html需要以下2个文件: 1.style.css div,span,p { width:140px; height:140px; margin:5px; background:#aaa; bo ...
- 学习块格式化上下文(BlockFormattingContext)
什么是BFC BFC全称是Block Formatting Context,即块格式化上下文.它是CSS2.1规范定义的,关于CSS渲染定位的一个概念.要明白BFC到底是什么,首先来看看什么是视觉格式 ...
- Java实战之03Spring-01Spring概述
一.Spring概述 1.Spring是什么? Spring是分层的Java SE/EE应用 full-stack轻量级开源框架,以IoC(Inverse Of Control:反转控制)和AOP(A ...
- UTF-8/UTF-16/UTF-32
UTF-8/UTF-16/UTF-32 一.UTF-8/UTF-16/UTF-32三者的区别 二.BOM的检测与删除 1.用VIM去除<feff>,即 U+FEFF.注意:这是一个字符,而 ...
- JS 版的pnp in_array($str,$arr)
var a = Array(1,2,3,4,5); function in_array(search,array){ for(var i in array){ if(array[i]==search) ...
- HTMLImageElement类型的简便利用
这个是我在复习书籍的时候看见的,当时一个同学想通过页面发送请求,但是数据量不是太大,所以用的get方式,但是页面用表单提交请求的话会让页面进行跳转,当时我在网上查了一点资料,发现基本上都是通过ajax ...
- 【仿携程JQuery日期价格表】
今天比较闲所以就花点时间又写了点东西. 相信这种价格表大家不会陌生 现在我就模仿它做一个简单版本的.效果如下 首先需要两个时间控件,我这里用的是HTML5里面的时间控件,这个没限制喜欢用什么就用什么 ...
- Git 安装与使用(一)
一.简介 1.集中式版本控制系统(CVS):Svn * 版本库是集中存放在中央服务器中的,客户端需要先从中央服务器取得最新的版本,然后再干活,活干完后,再把自己的代码推送给中央服务器. ...
- centos用yum安装mysql-server
1.安装:#yum -y install mysql-server 2.修改配置:#vi /etc/my.cnf 暂时修改一下编码(添加在密码下方添加): default-character-set ...
- 手机的touch事件(基于jquery)
javascript代码: $.swipe=function(opt){ var o = $.extend({ mainSelector:"", swipeLe ...