【POJ 3140】 Contestants Division
【题目链接】
【算法】
树形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的更多相关文章
- 【POJ 3140】 Contestants Division(树型dp)
id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS Memory Limit: 65536K Tot ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- POJ 1006-Biorhythms,中国剩余定理,学信安的路过!
Biorhythms 我竟然1A了, 终于从一天的浑噩中找回点自信了.人生第一次做中国剩余定理的题 ...
- CSU 1307 最短路+二分
题目大意: 帮忙找到一条a到b的最短路,前提是要保证路上经过的站点的最大距离尽可能短 这道题居然要用到二分...完全没去想过,现在想想求最大距离的最小值确实是... 这里不断二分出值代入spfa()或 ...
- react.js 父子组件数据绑定实时通讯
import React,{Component} from 'react' import ReactDOM from 'react-dom' class ChildCounter extends Co ...
- NodeJS仿WebApi路由
用过WebApi或Asp.net MVC的都知道微软的路由设计得非常好,十分方便,也十分灵活.虽然个人看来是有的太灵活了,team内的不同开发很容易使用不同的路由方式而显得有点混乱. 不过这不是重点, ...
- PDO的基本操作
PDO的基本操作 连接到mysql: try { $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); foreach($ ...
- js去除字符串两边的空格
bless = bless.replace(/(^\s*)|(\s*$)/g, "") // 去掉两边的空格
- [bzoj3998][TJOI2015]弦论_后缀自动机
弦论 bzoj-3998 TJOI-2015 题目大意:给定一个字符串,求其$k$小子串. 注释:$1\le length \le 5\cdot 10^5$,$1\le k\le 10^9$. 想法: ...
- hihocoder 1579(排列组合)
题意 给出一个长度为n的字符串的sa数组,n<=1e5,问有多少种不同的字符串的sa数组正好是输入的sa数组(字符串每个位置都是小写字母) 分析 sa数组描述的是字符的大小关系,而不是确切的字符 ...
- ZXing 二维码解析生成工具类
原文:http://www.open-open.com/code/view/1455848023292 import com.google.zxing.*; import com.google.zxi ...
- setenv LD_LIBRARY_PATH
For most Linux binaries, NCL was built using gcc and gfortran. This may cause a dependency on a file ...