链接

这叫树形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的更多相关文章

  1. python from __future__ import division

    1.在python2 中导入未来的支持的语言特征中division(精确除法),即from __future__ import division ,当我们在程序中没有导入该特征时,"/&qu ...

  2. [LeetCode] Evaluate Division 求除法表达式的值

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  3. 关于分工的思考 (Thoughts on Division of Labor)

    Did you ever have the feeling that adding people doesn't help in software development? Did you ever ...

  4. POJ 3140 Contestants Division 树形DP

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

  5. 暴力枚举 UVA 725 Division

    题目传送门 /* 暴力:对于每一个数都判断,是否数字全都使用过一遍 */ #include <cstdio> #include <iostream> #include < ...

  6. GDC2016【全境封锁(Tom Clancy's The Division)】对为何对应Eye Tracked System,以及各种优点的演讲报告

    GDC2016[全境封锁(Tom Clancy's The Division)]对为何对应Eye Tracked System,以及各种优点的演讲报告 原文 4Gamer編集部:松本隆一 http:/ ...

  7. Leetcode: Evaluate Division

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  8. 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 ...

  9. uva 725 Division(暴力模拟)

    Division 紫书入门级别的暴力,可我还是写了好长时间 = = [题目链接]uva 725 [题目类型]化简暴力 &题解: 首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数 ...

随机推荐

  1. ios--socket

    一.打开服务器 a.在终端打开,到服务器文件路径输入命令 python chatserver.py b.当显示 Iphone Chat server started 表示成功 二.建立连接 a.设置对 ...

  2. 12天学好C语言——记录我的C语言学习之路(Day 9)

    12天学好C语言--记录我的C语言学习之路 Day 9: 函数部分告一段落,但是我们并不是把函数完全放下,因为函数无处不在,我们今后的程序仍然会大量运用到函数 //转入指针部分的学习,了解指针是什么 ...

  3. java中的异常处理机制_finally的使用

    finally总结: finally代码块:定义一定执行的代码 通常用于关闭资源或者某些一定执行的代码 实例1:finally功能演示 class FuShuException extends Exc ...

  4. 此一生 一个纯js的ajax

    /** * 得到ajax对象 */ function getajaxHttp() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp ...

  5. W3C盒子与IE盒子模型

    盒模型: 内容(content).填充(padding).边界(margin). 边框(border) IE的content部分把 border 和 padding计算了进去 例:一个盒子的 marg ...

  6. SQL技巧之分类汇总

    数据表结构username type numaaaa   玉米 1212aaaa   玉米  212bbb     小麦  2323bbb .... 只有两种产品 玉米和小麦,玉米价格1.5,小麦价格 ...

  7. ASP.NET输入文本框自动提示功能

    在ASP.NET Web开发中会经常用到自动提示功能,比如百度搜索.我们只要输入相应的关键字,就可以自动得到相似搜索关键字的提示,方便我们快速的输入关键字进行查询. 那么在ASP.NET中,如果我们需 ...

  8. 点击UserControl中的按钮将值传给winform页面

    源码下载地址:http://download.csdn.net/detail/dora_zhh/7456521 1.如图所示,点击选择按钮弹出用户控件UserControl 2.点击确定按钮,将值传给 ...

  9. MVC , MVP , MVVM【转 阮一峰的网络日志】

    一.MVC MVC模式的意思是,软件可以分成三个部分. 视图(View):用户界面. 控制器(Controller):业务逻辑 模型(Model):数据保存 各部分之间的通信方式如下. View 传送 ...

  10. Cassandra1.2文档学习(3)——数据分配和复制

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/architecture/a ...