题目链接

题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯。

2B错误,导致WA了多次,无向图,建图搞成了有向了....

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define LL __int64
struct node
{
int u,v,next;
}edge[];
LL p[];
LL dp[];
int first[];
int o[];
int t;
LL ans,sum;
void CL()
{
t = ;
memset(first,-,sizeof(first));
memset(dp,,sizeof(dp));
memset(o,,sizeof(o));
}
void add(int u,int v)
{
edge[t].u = u;
edge[t].v = v;
edge[t].next = first[u];
first[u] = t ++;
}
void dfs(int x)
{
int i;
LL minz,res;
if(o[x]) return ;
o[x] = ;
res = p[x];
for(i = first[x];i != -;i = edge[i].next)
{
dfs(edge[i].v);
res += dp[edge[i].v];
}
dp[x] = res;
minz = (sum - res) - res;
if(minz < ) minz = -minz;
if(ans > minz)
ans = minz;
}
int main()
{
int n,m,i,u,v,cas = ;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(!n && !m) break;
sum = ;
CL();
for(i = ;i <= n;i ++)
{
scanf("%I64d",&p[i]);
sum += p[i];
}
ans = sum;
for(i = ;i < m;i ++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
for(i = ;i <= n;i ++)
{
if(!o[i])
{
dfs(i);
}
}
printf("Case %d: %I64d\n",cas++,ans);
}
return ;
}

POJ 3140 Contestants Division的更多相关文章

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

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

  2. POJ 3140 Contestants Division 树形DP

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

  3. POJ 3140 Contestants Division (树dp)

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

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

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

  5. POJ 3140 Contestants Division 【树形DP】

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

  6. poj 3140 Contestants Division [DFS]

    题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小. 思路:这个题我直接dfs做的,不知道树状dp是什么思路..一开始看到数据规模有些后怕,后来想到long long 可以达 ...

  7. POJ 3140 Contestants Division (树形DP,简单)

    题意: 有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小.问差的绝对值.(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1) 思路: ...

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

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

  9. POJ 3104 Contestants Division

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

随机推荐

  1. NYOJ 106背包问题

    http://acm.nyist.net/JudgeOnline/problem.php?pid=106 背包问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现 ...

  2. apache2:Invalid option to WSGI daemon process definition

    版本说明: ubuntu 12.04 server /apache 2.2 / mod_wsgi 3.3 / python 2.7.3 /django 1.7 在ubuntu12的服务器上配置djan ...

  3. django admin 扩展

    添加自定义动作: 例子,添加一个方法,批量更新文章,代码如下: from django.contrib import admin from myapp.models import Article de ...

  4. 取出type="button" 和type="text" 里面的值显示在页面

    <script  type="text/JavaScript> function changeLink() { document.getElementById("nod ...

  5. #define 的一些用法 以及 迭代器的 [] 与 find()函数的区别

    #include "stdafx.h" #include <map> #include <string> #include <iostream> ...

  6. 45. Singleton类的C++/C#实现[Singleton]

    [题目] 设计一个类,我们只能生成该类的一个实例. [分析] 单例模式的意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点.让类自身负责保存它的唯一实例.这个类可以保证没有其他实例可.以被创建 ...

  7. Java for LeetCode 162 Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  8. ORACLE恢复删除的数据

    ---正在执行的 select a.username, a.sid,b.SQL_TEXT, b.SQL_FULLTEXT  from v$session a, v$sqlarea b where a. ...

  9. css3学习总结5--CSS3文本效果

    CSS3 文本效果 1. text-shadow 2. word-wrap text-shadow属性使用方法 text-shadow属性使用方法如下所示. text-shadow:length le ...

  10. 转载——用Mixer API函数调节控制面板的音频设置

    关键词:Mixer函数,控制面板,音频设备调节 如果你用过windows的音频设备,比如播放音乐或者录音,聊天,调节麦克或者声音的大小,以及设置静音,都可以通过控制面板中的音频设置面板来调节,你对于下 ...