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. Ubuntu下查看APT安装的软件安装路径和版本

    1.查询安装路径 dpkg -L 软件名 例如:dpkg -L gedit dpkg -L gedit /. /usr /usr/bin /usr/bin/gedit /usr/share /usr/ ...

  2. 移动端调试神器(eruda)

    在日常的移动端开发时,一般都是试用chrome浏览器的移动端模式进行开发和调试,只有在chrome调试完成,没有问题了才会上到真机测试,移动端开发的一大问题就在于此, 各种品牌各种型号手机,手机中各种 ...

  3. 在XC2440的uboot中挂载U盘,利用FAT文件系统读写U盘文件

    转:http://blog.chinaunix.net/uid-22030783-id-3347608.html 在XC2440的uboot_V1.3版本中已经支持USB HOST驱动和FAT文件系统 ...

  4. vagrant public_network 自定义静态 ip配置方法

    Vagrant 创建虚拟化开发环境 Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境.它 使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境. ...

  5. Android 垃圾回收,用软引用建立缓存

    内存对于手机来说是非常重要的. 下面总结了我们在注意创建对象时的规则,以及怎么更好更快的实行GC回收,和怎么构建高速的对象cace缓冲. 1 避免循环遍历的创建对象,哪怕对象很小,也是要占资源的. 2 ...

  6. Android内存优化8 内存检测工具2 LeakCanary——直白的展现Android中的内存泄露

    之前碰到的OOM问题,终于很直白的呈现在我的眼前:我尝试了MAT,但是发现不怎么会用.直到今天终于发现了这个新工具: 当我们的App中存在内存泄露时会在通知栏弹出通知: 当点击该通知时,会跳转到具体的 ...

  7. android使用C/C++调用SO库

    有时候,我们反编译apk得到一个so库,如果直接使用这个so库的话,必须使用原来so库同样的package名字,才能用.这样人家反编译你的apk,就知道你侵犯了人家的版权.为了达到混淆的目的,我们可以 ...

  8. Tile based Rendering 二 tbr and tbdr 优化建议tiled based deferred rendering

    http://www.seas.upenn.edu/~pcozzi/OpenGLInsights/OpenGLInsights-TileBasedArchitectures.pdf tbr 和tbdr ...

  9. SQL Server 根据表名取得 表主键

    exec sp_primary_keys_rowset N'表名',NULL

  10. Android-经常涉及到的权限

    Android中配置权限的方法: 在AndroidMainFest.xml中加上以下代码 Android中一些经常涉及到的权限: 添加WiFi以及访问网络的权限: <uses-permissio ...