【题目链接】

点击打开链接

【算法】

树形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. [POJ1984]Navigation Nightmare

    [POJ1984]Navigation Nightmare 试题描述 Farmer John's pastoral neighborhood has N farms (2 <= N <= ...

  2. 【二分+尺取】HDU 6119 小小粉丝度度熊

    http://acm.hdu.edu.cn/showproblem.php?pid=6119 [思路] 首先通过处理交叉的可以处理成不交叉的 然后二分查找答案 如何判断一个长度是否可行? 双指针O(n ...

  3. fedora安装后,yum命令不能使用,Cannot retrieve metalink for repository: fedora. Please verify its path and try again 解决方法

    fedora安装后,yum命令不能使用,Cannot retrieve metalink for repository: fedora. Please verify its path and try ...

  4. unbuntu下安装多个JAVA JDK版本及如何切换

    当前环境已经安装过jdk1.6.0_45安装JDK 1.7.x时,若安装错误,可执行以下步骤:sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-ge ...

  5. ES6__函数的扩展

    /** * 函数的扩展 * 1 为函数参数指定默认值 * 2 函数的 rest 参数 * 3 箭头函数 */ // ------------------------------------------ ...

  6. linux命令2——进程相关

    (1)ps  -ef :可以看到内核的线程.

  7. python学习之-- redis模块基本介绍

    数据缓存系统: 1:mongodb:是直接持久化,直接存储于硬盘的缓存系统 2:redis: 半持久化,存储于内存和硬盘 3:memcache:数据只能存储在内存里的缓存系统关于memcache 学习 ...

  8. 最近公共祖先(Least Common Ancestors)

    题意: 给定一棵有根树T,给出若干个查询lca(u, v)(通常查询数量较大),每次求树T中两个顶点u和v的最近公共祖先,即找一个节点,同时是u和v的祖先,并且深度尽可能大(尽可能远离树根).通常有以 ...

  9. Piggy-Bank--hdu1114(完全背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=1114 Problem Description Before ACM can do anything, a bud ...

  10. The Unique MST-POJ1679(次小生成树)

    http://poj.org/problem?id=1679 次小生成树 #include<stdio.h> #include<string.h> #include<st ...