POJ -- 3140
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define MAXN 1000010
#define LL long long
const LL INF = 0x7fffffffffffff;
using namespace std;
typedef struct{
int to,next;
}Node;
Node edge[*MAXN];
int vis[MAXN/],head[MAXN/],val[MAXN/];
LL sum[MAXN/],all_sum,ans;
LL My_abs(LL tmp){return tmp < ? -tmp : tmp;}
void addedge(int u,int v,int k){
edge[k].to = v;
edge[k].next = head[u];
head[u] = k;
}
LL dfs_sum(int s){
vis[s] = ;
sum[s] = val[s];
for(int i = head[s];~i;i = edge[i].next){
int u = edge[i].to;
if(!vis[u]) sum[s] += dfs_sum(u);
}
return sum[s];
}
void dfs_minabs(int s){
vis[s]= ;
for(int i = head[s];~i;i = edge[i].next){
int u = edge[i].to;
if(!vis[u]){
dfs_minabs(u);
ans = min(ans,My_abs(all_sum - *sum[u]));
}
}
}
int main(){
int n,m,cas = ;
freopen("in.c","r",stdin);
while(~scanf("%d%d",&n,&m) && (m+n) ){
int k = ,tmp,u,v;
all_sum = ;
for(int i = ;i <= n;i ++) scanf("%d",&tmp),val[i] = tmp,all_sum += tmp;
memset(head,-,sizeof(head));
for(int i = ;i < m;i ++){
scanf("%d%d",&u,&v);
addedge(u,v,k++);
addedge(v,u,k++);
}
memset(vis,,sizeof(vis));
dfs_sum();
ans = INF;
memset(vis,,sizeof(vis));
dfs_minabs();
if(n == ) ans = val[];
printf("Case %d: %lld\n",++cas,ans);
}
return ;
}
POJ -- 3140的更多相关文章
- 【POJ 3140】 Contestants Division(树型dp)
id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS Memory Limit: 65536K Tot ...
- poj 3140(树形dp)
题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...
- POJ 3140.Contestants Division 基础树形dp
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10704 Accepted: ...
- poj 3140 树形去边差异最小
http://poj.org/problem?id=3140 依然是差异最小问题,不过这次是去边.思路是这样的,先记录每个点的子节点个数,然后遍历每个边. 有两个问题要注意: abs可能会出编译适配问 ...
- POJ 3140 Contestants Division (树dp)
题目链接:http://poj.org/problem?id=3140 题意: 给你一棵树,问你删去一条边,形成的两棵子树的节点权值之差最小是多少. 思路: dfs #include <iost ...
- POJ 3140 Contestants Division 树形DP
Contestants Division Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...
- POJ 3140 Contestants Division
题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- POJ 3140 Contestants Division 【树形DP】
<题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...
随机推荐
- leetcode342——Power of Four(C++)
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- QListWidget 删除选中项目
void MainWindow::on_action_Del_triggered() { QList<QListWidgetItem*> selectedItems = ui->li ...
- 九度OJ 1525 子串逆序打印 -- 2012年Google校园招聘笔试题目
题目地址:http://ac.jobdu.com/problem.php?pid=1525 题目描述: 小明手中有很多字符串卡片,每个字符串中都包含有多个连续的空格,而且这些卡片在印刷的过程中将字符串 ...
- 利用Xlinix SDK 建立Linux程序以及对该程序进行调试
一.创建Linux程序 1. 点击File > New > Application Project .并参照下图设置. 2. 输入工程名,并选择存储路径. 3. 选择所需的操作系统平台(O ...
- 服务器卡死,重启报错: INFO: task blocked for more than 120 seconds
问题:服务器负载很高,但是CPU利用率不高.服务器经常夯住,网站打不开,SSH连接非常不稳定,输入命令夯住. 重启服务器报错: INFO: task blocked for more than 120 ...
- Php之Http请求urlencode/rawurlencode
在http请求中,如果参数值带中文最好使用urlencode/rawurlencode函数. 如果参数值中带加号也最好使用,否则后台接收时,+号变成空格,引起不必要的麻烦. 注:urlencode和r ...
- php date('Y')
date('Y')默认是y-12-01 date('Y-01')!!!才是我需要的
- PHP源代码分析(第一章):Zend HashTable详解【转】
转载于http://www.phppan.com/2009/12/zend-hashtable/ 在PHP的Zend引擎中,有一个数据结构非常重要,它无处不在,是PHP数据存储的核心,各种常量.变量. ...
- SimpleMembership: The future of membership for ASP.NET
http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal- ...
- WordPress 使用 Pie-Register 添加前台注册、登录、找回密码和编辑个人资料功能
转自:http://www.wpdaxue.com/front-end-publishing.html Pie-Register 是一个功能比较完善的 WordPress 才能,使用它可以很方便添加和 ...