【题目链接】

点击打开链接

【算法】

树形DP

ans = min{ | total - 2 * sum[k] | } (sum为以k为根的子树的权值和)

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 1000010
const long long INF = 1e13; long long i,x,y,n,m,tot,ans,TC;
vector<long long> e[MAXN];
long long sum[MAXN],val[MAXN]; inline long long abs(long long x) { return x < ? -x : x; }
inline void dfs(long long x,long long fa)
{
long long i,y;
sum[x] = val[x];
for (i = ; i < e[x].size(); i++)
{
y = e[x][i];
if (fa == y) continue;
dfs(y,x);
sum[x] += sum[y];
}
if (abs(tot-sum[x]*) < ans) ans = abs(tot-sum[x]*);
} int main()
{ while (scanf("%lld%lld",&n,&m) != EOF)
{
tot = ;
ans = INF;
if (!n && !m) break;
for (i = ; i <= n; i++) e[i].clear();
for (i = ; i <= n; i++)
{
scanf("%lld",&val[i]);
tot += val[i];
}
for (i = ; i <= m; i++)
{
scanf("%lld%lld",&x,&y);
e[x].push_back(y);
e[y].push_back(x);
}
dfs(,);
printf("Case %lld: %lld\n",++TC,ans);
} return ;
}

【POJ 3140】 Contestants Division的更多相关文章

  1. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  2. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  3. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  4. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  5. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  6. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  7. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  8. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  9. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

随机推荐

  1. POJ-1067取石子游戏,威佐夫博弈范例题/NYOJ-161,主要在于这个黄金公式~~

    取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K              Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取 ...

  2. 病毒的侵扰和再侵扰两道AC自动机的应用

    HDU2896 病毒的侵扰 http://vjudge.net/problem/viewProblem.action?id=16404 题目大意: 记录每个病毒的编号,并给出一些网站的源码,分别输出网 ...

  3. 【尺取】HDU Problem Killer

    acm.hdu.edu.cn/showproblem.php?pid=5328 [题意] 给定一个长度为n的正整数序列,选出一个连续子序列,这个子序列是等差数列或者等比数列,问这样的连续子序列最长是多 ...

  4. openGL加载obj文件+绘制大脑表层+高亮染色

    绘制大脑表层并高亮染色的工作是以openGL加载obj文件为基础的,这里是我们用到的原始程序:只能加载一个obj文件的demo. 然而,一个完整的大脑表层是由很多分区组成的,因此我们的程序需要支持两个 ...

  5. Contest Hunter #46 T1 磁力块 [花式暴力]

    将所有石头按距离远近排序,将所有取到的时候扔进堆里维护最大磁力强度. 贪心,每次用强度最强的磁石尝试吸引地上的石头,扫完区间以后,这块石头就再也不会用到了. 在此基础上可以做些小优化,比如说优化未取石 ...

  6. 新建一个基于vue.js+Mint UI的项目

    上篇文章里面讲到如何新建一个基于vue,js的项目(详细文章请戳用Vue创建一个新的项目). 该项目如果需要组件等都需要自己去写,今天就学习一下如何新建一个基于vue.js+Mint UI的项目,直接 ...

  7. linux 安装问题make: 没有指明目标并且找不到makefile。 停止

    错误发生的可能原因,忘记安装软件需要的依赖.

  8. ***ps -ef |grep 输出的具体含义是什么?

    Q: 比如:[root@localhost ~]# ps -ef | grep ApacheJetspeedroot 18887 18828 0 08:09 pts/0 00:00:00 grep A ...

  9. freeswitch三方通话配置

    此种方法能实现,其中默认转移后按0,可进入三方通话. 用transfer只能实现代接转移. Misc. Dialplan Tools att xfer From FreeSWITCH Wiki Jum ...

  10. BloomFilter学习

    看大数据面试题,看到BloomFilter,找了篇文章学习一下: http://www.cnblogs.com/heaad/archive/2011/01/02/1924195.html Bloom ...