Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9121   Accepted: 2623

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

发现训练计划树dp的难度是递减的……不要说什么做多了熟练了。。真的是递减的。。这个非常适合入门……结果就被垫底了,。可能不是有意的……但像我这样的喜欢从上往下刷的………………



题目大意:几个学校间有连接,而且保证是树状连接,如今要在某条线路(树边)上搭载系统,为了降低负荷,要让系统两边的学生数量差值尽量少

dfs的时候能够遍历到全部的边。这样每条边的孩子所在的子树的全部节点权值(学生数)非常easy求出,然后用总学生数减去它。就是树边的还有一边的全部学生数了。



代码例如以下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8; struct Edge
{
int v,next;
}; Edge eg[2333333];
int head[233333];
int val[233333];
LL dp[233333];
int n,m;
LL sum,mn; void dfs(int u,int pre)
{
dp[u] = val[u];
for(int i = head[u]; i != -1; i = eg[i].next)
{
int v = eg[i].v;
if(v == pre) continue;
dfs(v,u);
LL tmp = sum-dp[v]*2;
if(tmp < 0) tmp = -tmp;
if(mn == -1) mn = tmp;
else mn = min(mn,tmp);
dp[u] += dp[v];
}
} int main()
{
//fread();
//fwrite(); int u,v,z = 1; while(~scanf("%d%d",&n,&m) && (n+m))
{
sum = 0;
for(int i = 1; i <= n; ++i)
{
scanf("%d",&val[i]);
sum += val[i];
} int tp = 0;
memset(head,-1,sizeof(head));
for(int i = 0; i < m; ++i)
{
scanf("%d%d",&u,&v);
eg[tp].v = v;
eg[tp].next = head[u];
head[u] = tp++;
eg[tp].v = u;
eg[tp].next = head[v];
head[v] = tp++;
} mn = -1;
dfs(1,1);
printf("Case %d: %lld\n",z++,mn);
} return 0;
}

【POJ 3140】 Contestants Division(树型dp)的更多相关文章

  1. POJ 3140.Contestants Division 基础树形dp

    Contestants Division Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10704   Accepted:  ...

  2. POJ 3140 Contestants Division (树dp)

    题目链接:http://poj.org/problem?id=3140 题意: 给你一棵树,问你删去一条边,形成的两棵子树的节点权值之差最小是多少. 思路: dfs #include <iost ...

  3. poj 3140 Contestants Division(树形dp? dfs计数+枚举)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  4. POJ 3140 Contestants Division 【树形DP】

    <题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...

  5. POJ 3140 Contestants Division (树形DP,简单)

    题意: 有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小.问差的绝对值.(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1) 思路: ...

  6. POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)

    POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...

  7. POJ 3140 Contestants Division 树形DP

    Contestants Division   Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...

  8. POJ 3140 Contestants Division

    题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...

  9. POJ 1947 - Rebuilding Roads 树型DP(泛化背包转移)..

    dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t)  < i=(1,n) , t = i是否为整棵树的根 > 更新的时 ...

  10. poj 3140 Contestants Division [DFS]

    题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小. 思路:这个题我直接dfs做的,不知道树状dp是什么思路..一开始看到数据规模有些后怕,后来想到long long 可以达 ...

随机推荐

  1. Codeforces Round #114 (Div. 1) D. Wizards and Roads 笛卡尔树+树贪心+阅读题

    D. Wizards and Roads 题目连接: http://www.codeforces.com/contest/167/problem/D Description In some count ...

  2. GIT使用(自用)

    1.远程分支就是本地分支push到服务器上的时候产生的.比如master就是一个最典型的远程分支(默认). 1 $: git push origin master 除了master之外,我们还可以随便 ...

  3. RapidXml使用方法

    一.写xml 文件 [cpp] view plaincopy #include <iostream> #include "rapidxml/rapidxml.hpp" ...

  4. 项目中集成Easyui-Tree,可拖拽更新节点

    由于公司架构较旧,使用的jdk版本为1.4,页面上也没有el表达式. Html <ul class="easyui-tree" id="data"> ...

  5. PostgreSQL配置文件--AUTOVACUUM参数

    8 AUTOVACUUM参数 AUTOVACUUM PARAMETERS 8.1 autovacuum 字符型 默认: autovacuum = on Enable autovacuum subpro ...

  6. HTML 标签的 enctype 属性

    定义和用法 enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 默认地,表单数据会编码为 "application/x-www-form-urlencoded" ...

  7. BZOJ3157: 国王奇遇记 & 3516: 国王奇遇记加强版

    令\[S_i=\sum_{k=1}^n k^i m^k\]我们有\[\begin{eqnarray*}(m-1)S_i & = & mS_i - S_i \\& = & ...

  8. Enyim Memached 客户端 执行GET 总是返回NULL

    排查: 1. ping 远程Linux 服务器 正常 2.11211 端口正常 3. ps aux | grep memcached 显示正常 4. 使用另外一个客户端 memcachedClient ...

  9. nginx最大并发连接数的思考:worker_processes、worker_connections、worker_rlimit_nofile

    参考nginx官网:http://nginx.org/en/docs/ngx_core_module.html#worker_connections 从用户的角度,http 1.1协议下,由于浏览器默 ...

  10. c# string与String区别

    [C#] String与string的区别:供参考 转自:https://www.cnblogs.com/rosesmall/p/8351808.html C#是区分大小写的,但是我却发现C#中同时存 ...