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. epoll 浅析以及 nio 中的 Selector

    首先介绍下epoll的基本原理,网上有很多版本,这里选择一个个人觉得相对清晰的讲解(详情见reference): 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行I/O操作 ...

  2. 电话号码 【trie树】

    电话号码 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描写叙述 给你一些电话号码,请推断它们是否是一致的,即是否有某个电话是还有一个电话的前缀. 比方: Emerg ...

  3. 【Caffe代码解析】compute_image_mean

    功能: 计算训练数据库的平均图像. 由于平均归一化训练图像会对结果有提升,所以Caffe里面,提供了一个可选项. 用法: compute_image_mean [FLAGS] INPUT_DB [OU ...

  4. 改进后的向量空间模型(VSM)

    我们採用更加形式化的定义,并採用稍大一些的样例来展示怎样使用基于数据集频率的权重.相应于一个给定的词项,其权重使用IDF(逆文档频率)来计算. 为了给每篇文档建立一个相应的向量,能够考虑例如以下定义. ...

  5. Linux退出时出现there are stopped jobs如何解决?

    Linux 使用exit时出现there are stopped jobs如何解决? 这是因为一些命令被挂起了, 在后台驻留,需要关闭. 解决问题: 输入命令jobs -l显示停止进程的详细列表 可以 ...

  6. 科学计算 | Matlab 使用 GPU 并行计算

    科学计算 | Matlab 使用 GPU 并行计算 本文转载自:  https://sanwen8.cn/p/14bJc10.html       Matlab下直接使用GPU并行计算(预告)< ...

  7. 关于input:-webkit-autofill样式问题

    最近在整理项目的时候,遇到了一个chrome浏览器自动填充的样式问题, 用户名跟密码的input都设置为透明颜色,但是会变成黄色,打开chrome调试工具,发现有个input:-webkit-auto ...

  8. 使用sed来自动注释/恢复crontab中的一个任务

    # 注释crontab任务crontab -l  >  ${WORK_DIR}/cron_binarysed  -i 's%\(.*/home/xyz/xyz.sh\)%#\1%' ${WORK ...

  9. mongodb 安装、启动

    MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1   最近有太多的同学向我提起MongoDB,想要学习MongoDB,还不知道MongoDB到底是什么鬼,或者说,知道是数据 ...

  10. A Short Introduction to Boosting

    http://www.site.uottawa.ca/~stan/csi5387/boost-tut-ppr.pdf Boosting is a general method for improvin ...