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

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

 
题意:删除一棵树上的一条边,使得形成的两棵树的权值差最小
思路:树形dp
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int > P;
const int N=2e5+,M=2e6+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
struct edge
{
int from,to;
int next;
};
edge es[M];
int cnt,head[N];
ll sum,ans;
ll w[N],deep[N];
void init()
{
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v)
{
cnt++;
es[cnt].from=u,es[cnt].to=v;
es[cnt].next=head[u];
head[u]=cnt;
}
void dfs(int u,int fa)
{
deep[u]=w[u];
for(int i=head[u];i!=-;i=es[i].next)
{
int v=es[i].to;
if(v==fa) continue;
dfs(v,u);
deep[u]+=deep[v];
}
if(sum>=*deep[u]) ans=min(ans,sum-*deep[u]);
else ans=min(ans,*deep[u]-sum);
}
int main()
{
int Case=,n,m;
while(~scanf("%d%d",&n,&m))
{
if(n==&&m==) break;
init();
sum=,ans=INF;
for(int i=;i<=n;i++) scanf("%lld",&w[i]),sum+=w[i];
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v),addedge(v,u);
}
memset(deep,0LL,sizeof(deep));
dfs(,);
printf("Case %d: %lld\n",++Case,ans);
}
return ;
}

树形dp

POJ 3140.Contestants Division 基础树形dp的更多相关文章

  1. POJ 3140 Contestants Division 【树形DP】

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

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

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

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

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

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

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

  5. POJ 3140 Contestants Division 树形DP

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

  6. POJ 3140 Contestants Division (树dp)

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

  7. POJ 3140 Contestants Division

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

  8. poj 3140 Contestants Division [DFS]

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

  9. 基础树形DP小结

    HDU 4044 Geodefense http://blog.csdn.net/zmx354/article/details/25109897 树形DP暂且先告一段落了. HDU 3586 Info ...

随机推荐

  1. Android 开发 Camera类的拍照与录像

    前言 在开发Android应用的时候,如果需要调用摄像头拍照或者录像,除了通过Intent调用系统现有相机应用进行拍照录像之外,还可以通过直接调用Camera硬件去去获取摄像头进行拍照录像的操作.本篇 ...

  2. 2018-2019-2 20165205 《网络对抗》 Exp5 MSF基础

    2018-2019-2 20165205 <网络对抗> Exp5 MSF基础 实验内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1 ...

  3. OGG-01091 Unable to open file "./dirdat/cs001481" (error 2, No such file or directory)

    记一次ogg报错ogg-01091的处理过程 现场有一套RAC数据库的服务器异常重启,其中一个节点上部署了OGG,机器开机之后,发现OGG所有应用进程abended状态,查看日志,发现报错如下图: 分 ...

  4. 【perl】simpleHTTP

    类似Python SimpleHTTPServer #!/usr/bin/perl # https://metacpan.org/pod/HTTP::Server::Simple # https:// ...

  5. Linux充电站

    [Ansible中文权威指南] [鸟哥的linux私房菜] AWK使用手册 Centos的epel源下载 Ceph国内社区 chef简明手册 ChinaUnix运维文库 Confluence和Jira ...

  6. python-day19 Django模板,路由分发,ORM

    @获取文件所有数据 request.FILES: request.POST.get('fafafa')#拿到文件名: user = request.POST.get('user',None)#用get ...

  7. Python初学(1)

    最近在学习python,以后想编写一些工作中用的到的脚本.python的入门我选择了<python从初学到入门>,这篇文章我会跟进我的学习进度.算是一个笔记吧. 我本身是熟悉C语言的,看p ...

  8. Mysql出现(10061)错误提示的暴力解决办法

    上个月我还在说别人的怎么老是会错呢,我的就没事,嘿 今天就轮到我了 我发誓 我绝对没碰它 是它先动的手 言归正传   下面给你们 介绍 终极大招  为什么是终极大招呢  因为网上那些前辈们的方法我都试 ...

  9. 64位的windows服务安装问题

    需要使用64位的安装exe文件才可以. @echo offC:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe -i &quo ...

  10. 【第二组】Hunter——beta版发布文档

    软件测试报告 一.bug情况汇总 尚需解决以及难以解决的: 登录时会有卡顿,需要加入加载进度条(会添加的) 商城和背包功能尚未实现(需要修复) 美工水平太差,让人没有使用的欲望(大概接下来就专门做这个 ...