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位数 ...
随机推荐
- 结构型模式——Bridge(未完成)
1.意图 将抽象部分与它的实现部分分离,使它们都可以独立地变化.
- cetnos 6.7 安装 oracle 11详解
CentOS 6.7下Oracle 11g安装详解 1. 安装环境 Linux服务器:CentOS 6.7 64位 Oracle数据库版本:Oracle 11gR2 64位 2. 配置修改及参数优 ...
- centos6.5 安装python2.7.5
1. 下载python2.7.5,保存到 /data/http://www.python.org/ftp/python/ 2. 解压文件tar xvf Python-2.7.5.tar.bz2 3. ...
- el-get
el-get Table of Contents 1. 依赖 2. 安装 3. 配置 3.1. 自定义包配置 4. 命令 5. 管理扩展 el-get 是一个emacs下的扩展管理工具.就像apt-g ...
- shell环境
1 引言 一个进程运行在shell环境中,理解进程运行的环境是十分重要的.环境影响着进程的行为,利用环境提供的便利,可以极大地提高开发效率.本节深入讨论shell中与进程有关的环境问题,包括命令行参数 ...
- linux共享文件samba安装与java读取外部文件夹方法
测试环境RedHat 6.4 一.安装 samba组件安装: (1)首先用“rpm –qa |grep samba”命令检验系统samba服务是否安装. #rpm –qa |grep samba sa ...
- 修改Sublime Text 3 的侧边栏字体大小
此文转载哈,忘了作者地址,还请见谅!! 首先需要确保安装了Package ControlPackage Control作为ST必备插件,这里就不多介绍了,没装的话,google一下,各种介绍以及安装教 ...
- Delphi中的GetEnumName和GetEnumValue的使用方法
利用TypInfo单元的GetEnumName和GetEnumValue可以遍历任意枚举类型,并获取其名称和值.下面是示例Demo. uses TypInfo; ... procedure TForm ...
- zoj 3765
一道区间更新.查询的题: 但是线段树不能做插入: 后来才知道用splay: splay用来做区间查询的话,先将l-1旋转到根节点,然后把r+1旋转到根节点的右节点: 这样的话,根节点的右节点的左子树就 ...
- android ListView内数据的动态添加与删除
main.xml 文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...