POJ 3104 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 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
#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的更多相关文章
- POJ 3140.Contestants Division 基础树形dp
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10704 Accepted: ...
- POJ 3140 Contestants Division 树形DP
Contestants Division Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...
- POJ 3140 Contestants Division (树dp)
题目链接:http://poj.org/problem?id=3140 题意: 给你一棵树,问你删去一条边,形成的两棵子树的节点权值之差最小是多少. 思路: dfs #include <iost ...
- POJ 3140 Contestants Division
题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- POJ 3140 Contestants Division 【树形DP】
<题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...
- poj 3140 Contestants Division [DFS]
题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小. 思路:这个题我直接dfs做的,不知道树状dp是什么思路..一开始看到数据规模有些后怕,后来想到long long 可以达 ...
- POJ 3140 Contestants Division (树形DP,简单)
题意: 有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小.问差的绝对值.(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1) 思路: ...
- POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)
POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...
随机推荐
- SWF代码分析与破解之路 (YueTai VIP视频信息获取工具) Socket续篇
引言 上一篇 <Socket与站点保密应用 (隐藏链接的视频下载)>大大咧咧地从 WEB 讲 Socket.再到 TCP/IP 等协议,又再讲到 Wireshark 怎样抓IP包分析.最还 ...
- Java多线程之~~~ReadWriteLock 读写分离的多线程实现
在多线程开发中,常常会出现一种情况,我们希望读写分离. 就是对于读取这个动作来说,能够同一时候有多个线程同 时去读取这个资源,可是对于写这个动作来说,仅仅能同一时候有一个线程来操作.并且同一时候,当有 ...
- android:使用gallery和imageSwitch制作可左右循环滑动的图片浏览器
为了使图片浏览器左右无限循环滑动 我们要自己定义gallery的adapter 假设要想自己定义adapter首先要了解这几个方法 @Override public int getCount() { ...
- 程序猿的量化交易之路(30)--Cointrader之ConfigUtil(17)
转载须注明出处:viewmode=contents">http://blog.csdn.net/minimicall?viewmode=contents.http://cloudtra ...
- SpringBoot学习之@Controller和@RestController
今天我们来研究一下@Controller和@RestController的用法 @Controller 1.Controller可以用来跳转页面,并且必须配合模板来使用. @Controller // ...
- Linux CenOS Python3 和 python2 共存
1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5. 使用 python -V 命令查看一下是否安 ...
- 今天遇到一个git码云同步的问题
一开始是因为eclipse编码不同意导致乱码,所以我和师弟就想统一都用UTF-8的,师弟统一好了,让我pull一下,pull的时候有冲突,因为我和师弟都修改了其中一个文件,然后我这边就删除了那个文件再 ...
- EhCache Monitor的使用
1.在http://ehcache.org/documentation/monitor.html#Installation_And_Configuration下载ehcache-monitor-kit ...
- uboot下载地址
非常奇怪百度搜索都搜不到一个好的uboot下载的说明,仅此标记 HOME http://www.denx.de/wiki/U-Boot/SourceCode sourcecode http://www ...
- 牛顿迭代法(Newton's Method)
牛顿迭代法(简称牛顿法)由英国著名的数学家牛顿爵士最早提出.可是,这 一方法在牛顿生前并未公开发表(讨厌的数学家们还是鼓捣出来了) 牛顿法的作用是使用迭代的方法来求解函数方程的根. 简单地说,牛顿法就 ...