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. Android应用程序的解析

    一: 文件架构 二: 图片,语音资源的使用 图片的两种使用方法: 第一种: 使用imageView控件 <ImageView android:id="@+id/imageView1&q ...

  2. Red Hat Enterprise Linux上配置SQL Server Always On Availability Group

    http://www.cnblogs.com/lavender000/p/6946848.html

  3. minGW cygwin gnuwin32

    首先,三个的官方网站分别是: minGW:http://www.mingw.org cygwin:  http://www.cygwin.com gnuwin32:  https://sourcefo ...

  4. [NHibernate]使用AttributeNHibernate.Mapping.Attributes

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...

  5. 三种常见的部署Kubernetes的方式

    三种常见的部署Kubernetes的方式 嘹亮的小号 Ghostcloud-001工号,资深Docker玩家,分布式系统研发11年. ​关注他   容器技术将应用程序及其依赖关系与操作系统进行分离,不 ...

  6. Selenium IDE 基础使用教程

    Selenium IDE 基础使用教程 简介及安装 Selenium IDE 是一个易于使用的Firefox插件.它提供了一个图形用户界面,可进行脚本录制及导出.其记录的脚本可以被转换成多种编程语言( ...

  7. .NET-DataColumn.DataType 属性

    public DataTable MakeDataTable(){ DataTable myTable; DataRow myNewRow; // Create a new DataTable. my ...

  8. webpack安装以及一些配置

    在用webpack之前... 或说没有实现组件化之前的web1.0时代! 最终迈向web2..0之后的时代! ===============华丽的分割线================== 安装步骤有 ...

  9. JAVA学习笔记 -- 读写XML

    XML是一种可扩展标记语言 以下是一个完整的XML文件(也是下文介绍读写XML的样本): <? xml version="1.0" encoding="UTF-8& ...

  10. 修改Jenkins的BUILD_NUMBER

    因为特殊原因需要修改jenkins的BUILD_NUMBER环境变量,现在将修改方法记录一下, Jenkins ->系统管理->脚本命令行 输入以下脚本,运行就Ok Jenkins.ins ...