<题目链接>

题目大意:
给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差。

解题分析:

比较基础的树形DP。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll;
#define INF 0x7fffffff7fffffff
#define clr(a,b) memset(a,b,sizeof(a))
#define rep(i,s,t) for(int i=s;i<=t;i++)
const int N = 1e5+;
struct Edge{
int to,nxt;
}edge[N<<];
int n,m,cnt,head[N];
ll val[N],dp[N],ans,sum; ll llabs(ll a){return a>=?a:(-a);} void init(){
cnt=;sum=;clr(head,-);
}
void addedge(int u,int v){
edge[cnt].to=v,edge[cnt].nxt=head[u];
head[u]=cnt++;
}
void dfs(int u,int pre){
dp[u]=val[u];
for(int i=head[u];~i;i=edge[i].nxt){
int v = edge[i].to;
if(v == pre)continue;
dfs(v,u);
dp[u]+=dp[v];
}
ans=min(ans,llabs(dp[u]-(sum-dp[u]))); //记录最小的差值
}
int main(){
int ncase=;
while(scanf("%d%d",&n,&m)!=EOF,n||m){
init();
rep(i,,n)scanf("%lld",&val[i]),sum+=val[i];
rep(i,,m){
int u,v;scanf("%d%d",&u,&v);
addedge(u,v);addedge(v,u);
}
ans=INF;
dfs(,-);
printf("Case %d: %lld\n",++ncase,ans);
}
}

2019-02-03

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)

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

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

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

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

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

  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. [poj3140]Contestants Division树形dp

    题意:切掉树上的某条边,使分开的两棵树上各点的权值和差值最小. 与hdu2196不同的是,此题是点权,其他无太大差别,注意数据范围. 先求出每个节点的子树权值和,然后自底向上dp即可.取$\min ( ...

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

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

随机推荐

  1. socket-WebSocket HttpListener TcpListener 服务端客户端的具体使用案例

    /// <summary>/// 启动服务监听的ip和端口的主线程/// </summary>/// <param name="tunnelPort" ...

  2. winform无需安装pdf阅读器打开pdf文件

    控件来源:http://www.o2sol.com/pdfview4net/download.htm (使用版本:2016年8月31号更新版) 备份链接: https://pan.baidu.com/ ...

  3. Confluence 6 考虑使用自定义 CSS

    CSS 的知识储备 如果你没有有关 CSS 的相关知识,请参考页面  CSS Resources section 中的内容.当你打算开始对 Confluence 的样式表进行修改之前,你应该对 CSS ...

  4. js模块化编程之CommonJS和AMD/CMD

    js模块化编程commonjs.AMD/CMD与ES6模块规范 一.CommonJS commonjs的require是运行时同步加载,es6的import是静态分析,是在编译时而不是在代码运行时.C ...

  5. metaclass(元类)

    一.创建类的执行流程 二.元类的认识 什么是元类呢?在Python3中继承type的就是元类 二.元类的示例 方式一: # 方式一 class MyType(type): '''继承type的就是元类 ...

  6. easyui合并多个单元格

    $('#table-v2').datagrid({ url: './data/am/data1_table.json', pagination: true, //显示分页 fit: true, //d ...

  7. vue指令

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>指令 ...

  8. springboot拦截器HandlerInterceptor详解

    Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截器). HandlerInterceptor 的功能跟过滤器类似,但 ...

  9. 数据结构C++实现代码-顺序表

    参考:https://blog.csdn.net/ebowtang/article/details/43094041 //seqList.h// //包含顺序表中的声明// #include<i ...

  10. 混合编译.c/.cpp与.cu文件

    混合编译.c/.cpp与.cu文件 项目中用到cuda编程,写了kernel函数,需要nvcc编译器来编译..c/.cpp的文件,假定用gcc编译. 如何混合编译它们,整体思路是:.cu文件编译出的东 ...