POJ 3140.Contestants Division 基础树形dp
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 10704 | Accepted: 3004 |
Description
In the new ACM-ICPC Regional Contest, a special monitoring and submitting system will be set up, and students will be able to compete at their own universities. However there’s one problem. Due to the high cost of the new judging system, the organizing committee can only afford to set the system up such that there will be only one way to transfer information from one university to another without passing the same university twice. The contestants will be divided into two connected regions, and the difference between the total numbers of students from two regions should be minimized. Can you help the juries to find the minimum difference?
Input
There are multiple test cases in the input file. Each test case starts with two integers N and M, (1 ≤ N ≤ 100000, 1 ≤ M ≤ 1000000), the number of universities and the number of direct communication line set up by the committee, respectively. Universities are numbered from 1 to N. The next line has N integers, the Kth integer is equal to the number of students in university numbered K. The number of students in any university does not exceed 100000000. Each of the following M lines has two integers s, t, and describes a communication line connecting university s and university t. All communication lines of this new system are bidirectional.
N = 0, M = 0 indicates the end of input and should not be processed by your program.
Output
For every test case, output one integer, the minimum absolute difference of students between two regions in the format as indicated in the sample output.
Sample Input
7 6
1 1 1 1 1 1 1
1 2
2 7
3 7
4 6
6 2
5 7
0 0
Sample Output
Case 1: 1
Source
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int > P;
const int N=2e5+,M=2e6+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
struct edge
{
int from,to;
int next;
};
edge es[M];
int cnt,head[N];
ll sum,ans;
ll w[N],deep[N];
void init()
{
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v)
{
cnt++;
es[cnt].from=u,es[cnt].to=v;
es[cnt].next=head[u];
head[u]=cnt;
}
void dfs(int u,int fa)
{
deep[u]=w[u];
for(int i=head[u];i!=-;i=es[i].next)
{
int v=es[i].to;
if(v==fa) continue;
dfs(v,u);
deep[u]+=deep[v];
}
if(sum>=*deep[u]) ans=min(ans,sum-*deep[u]);
else ans=min(ans,*deep[u]-sum);
}
int main()
{
int Case=,n,m;
while(~scanf("%d%d",&n,&m))
{
if(n==&&m==) break;
init();
sum=,ans=INF;
for(int i=;i<=n;i++) scanf("%lld",&w[i]),sum+=w[i];
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v),addedge(v,u);
}
memset(deep,0LL,sizeof(deep));
dfs(,);
printf("Case %d: %lld\n",++Case,ans);
}
return ;
}
树形dp
POJ 3140.Contestants Division 基础树形dp的更多相关文章
- POJ 3140 Contestants Division 【树形DP】
<题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- POJ 3140 Contestants Division (树形DP,简单)
题意: 有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小.问差的绝对值.(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1) 思路: ...
- POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)
POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...
- POJ 3140 Contestants Division 树形DP
Contestants Division Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...
- POJ 3140 Contestants Division (树dp)
题目链接:http://poj.org/problem?id=3140 题意: 给你一棵树,问你删去一条边,形成的两棵子树的节点权值之差最小是多少. 思路: dfs #include <iost ...
- POJ 3140 Contestants Division
题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...
- poj 3140 Contestants Division [DFS]
题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小. 思路:这个题我直接dfs做的,不知道树状dp是什么思路..一开始看到数据规模有些后怕,后来想到long long 可以达 ...
- 基础树形DP小结
HDU 4044 Geodefense http://blog.csdn.net/zmx354/article/details/25109897 树形DP暂且先告一段落了. HDU 3586 Info ...
随机推荐
- V先生:信息流广告标题党必备的500个热词
稍微没有全都偏,简直仅仅只永远, 已经曾经就竟然,将要立刻刚偶然, 渐渐终于决忽然,难道连续又再三, 也许必须很非常,最太十分更马上, 越极总挺常常再,屡次一定也不还. 你一定不知道.如何.最.咋.是 ...
- 字节码操作JAVAssist
字节码操作Javassist 字节码:字节码是设计被用来将代码高效的传送给多种软件平台.硬件平台,字节码的设计也实现了Java的平台无关性,字节码比机器码更抽象,它通常被认为是包含了一个可执行文件的二 ...
- Python第3天
字符串的魔法: expandtabs 可用来制表 当前输入是否为数字:isdecimal 最低级 isdigit 支持特殊数字 isnumeric 支持中文 标识符 isidentifier 是否存在 ...
- centos mysql 修改mysql用户密码
查看服务器版本: cat /etc/redhat-release 查看mysql 版本: mysql -u root -p use mysql; ###mysql 5.7以上.. update use ...
- abaqus2016安装过程中出现error:unable to add abaqus command directory to PATH variable
请问abaqus2016安装过程中出现error:unable to add abaqus command directory to PATH variable是什么原因,怎么解决啊,总是安装失败 这 ...
- 什么是P2P流标
1.被动流标:在规定的投标时间内,一般是7天,没有凑齐这笔借款,就流标了: 2.主动流标:借款人或平台原因,将为投满的标下架,做流标处理 介绍: 对于投资者来说,在投资P2P理财的时候,可能会遇到过流 ...
- Tinker热修复
集成buggly热修复的时候报错 Error:A problem occurred configuring project ‘:app’. Failed to notify project evalu ...
- Python 学习笔记01篇
编程基础很零碎 看了路飞学城的讲解视频,整体课程列表排列很清晰,每个视频都在十几分钟到二十几分钟之间,适合零碎化的的学习 第一章和第二章的前半部分可以较轻松地入门
- 1503.02531-Distilling the Knowledge in a Neural Network.md
原来交叉熵还有一个tempature,这个tempature有如下的定义: \[ q_i=\frac{e^{z_i/T}}{\sum_j{e^{z_j/T}}} \] 其中T就是tempature,一 ...
- 查找运行时间超过1天的frmweb进程
EBS中经常会出现一些zombie的frmweb进程,特征是运行时间很长,占用CPU都(1个frmweb进程占用1个CPU线程100%资源)下面脚本是利用awk找到运行时间超过1天的frmweb进程, ...