Contestants Division
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10597   Accepted: 2978

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 st, 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

 
题目大意:一棵树每个点有权值 求删掉某条边后 形成的两棵子树的权值差的绝对值最小。
 
题解 dfs
 
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define maxn 1000005
using namespace std; int n,m,x,y,sumedge,kse;
long long dad[maxn],w[maxn],size[maxn],head[maxn]; struct Edge{
int x,y,nxt;
Edge(int x=,int y=,int nxt=):
x(x),y(y),nxt(nxt){}
}edge[maxn<<]; void add(int x,int y){
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
} void dfs(int x){
size[x]=w[x];
for(int i=head[x];i;i=edge[i].nxt){
int v=edge[i].y;
if(v==dad[x])continue;
dad[v]=x;
dfs(v);
size[x]+=size[v];
}
} long long gg(long long x){
if(x<=)return -x;
return x;
} int main(){
while(~scanf("%d%d",&n,&m)){
if(!n&&!m)break;
memset(head,,sizeof(head));
memset(size,,sizeof(size));
memset(dad,,sizeof(dad));
for(int i=;i<=n;i++)scanf("%lld",&w[i]);
if(n==){printf("0\n");continue;}
for(int i=;i<n;i++)scanf("%d%d",&x,&y),add(x,y),add(y,x);
dfs();
__int64 ans=1e15;
for(int i=;i<=n;i++){
ans=min(ans,gg(gg(size[]-size[i])-size[i]));
}
printf("Case %d: %I64d\n",++kse,ans);
}
return ;
}
 

POJ 3104 Contestants Division的更多相关文章

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

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

  2. POJ 3140 Contestants Division 树形DP

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

  3. POJ 3140 Contestants Division (树dp)

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

  4. POJ 3140 Contestants Division

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

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

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

  6. POJ 3140 Contestants Division 【树形DP】

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

  7. poj 3140 Contestants Division [DFS]

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

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

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

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

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

随机推荐

  1. sql的一些知识_函数_汇总数据

    汇总数据 avg()---------求平均数 值得注意的是:avg()只能用于一个列的平均值查询,多个列的平均值请使用多个avg() avg()忽略null值 count()-------计数(指定 ...

  2. c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对

    首先要引用一下类库:using Ionic.Zip;这个类库可以到网上下载. 下面对类库使用的封装方法: 得到指定的输入流的ZIP压缩流对象 /// <summary> /// 得到指定的 ...

  3. 图像处理算法2——Otsu最佳阈值分割法http://blog.csdn.net/xiaqunfeng123/article/details/17121195

    http://blog.csdn.net/xiaqunfeng123/article/details/17121195Otsu法是1979年由日本大津提出的.该方法在类间方差最大的情况下是最佳的,即统 ...

  4. web前端面试系列 - js中的prototype

    js中一切皆为对象,其中函数也是一种对象, 而每个函数都有一个prototype属性,其值也是一个对象. 一.prototype的作用 1. 在多个实例对象之间共享数据和方法. 2. 通过原型链实现继 ...

  5. How to reset your password in Ubuntu

    There are many reasons you might want to reset a password: Someone gave you a computer with Ubuntu i ...

  6. 【转】VMware 11.0 简体中文版|附永久密钥

    VMware 11.0 简体中文版|附永久密钥 昨天,VMware虚拟机11.0 简体中文版正式发布,值得注意的是新版抛弃了32位系统支持,安装包体积大幅减小, 新增Windows 10 技术预览版支 ...

  7. 算法排序-NB三人组

    快速排序: 堆排序: 二叉树: 两种特殊二叉树: 二叉树的存储方式: 小结: 堆排序正题: 向下调整: 堆排序过程: 堆排序-内置模块: 扩展问题topk: 归并排序: 怎么使用: NB三人组小结

  8. 通过Pojo对象 field 属性加注解实现格式校验,极大的降低代码量

    近期做一个接口.接受外系统的报文,通过XStream转换成java对象以后.须要对当中的字段做格式校验. 要求例如以下: 传统的方式是硬编码校验.可是对于field非常多的情况.代码量暴增.easy出 ...

  9. Oracle学习(十三):闪回

    1.知识点:能够对比以下的录屏进行阅读 SQL> --1. 错误地删除了记录 SQL> --2. 错误地删除了表 SQL> --3. 查询历史记录 SQL> --4. 怎样撤销 ...

  10. session.use_cookies有什么作用,

    session.use_cookies有什么作用, @ini_set('session.use_cookies', 1);什么意思 若session.use_cookies = 1 sessionid ...