【POJ 3140】 Contestants Division(树型dp)
| 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)的更多相关文章
- POJ 3140.Contestants Division 基础树形dp
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10704 Accepted: ...
- POJ 3140 Contestants Division (树dp)
题目链接:http://poj.org/problem?id=3140 题意: 给你一棵树,问你删去一条边,形成的两棵子树的节点权值之差最小是多少. 思路: dfs #include <iost ...
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- POJ 3140 Contestants Division 【树形DP】
<题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...
- 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 ...
- POJ 3140 Contestants Division 树形DP
Contestants Division Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...
- POJ 3140 Contestants Division
题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...
- POJ 1947 - Rebuilding Roads 树型DP(泛化背包转移)..
dp[x][y]表示以x为根的子树要变成有y个点..最少需要减去的边树... 最终ans=max(dp[i][P]+t) < i=(1,n) , t = i是否为整棵树的根 > 更新的时 ...
- poj 3140 Contestants Division [DFS]
题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小. 思路:这个题我直接dfs做的,不知道树状dp是什么思路..一开始看到数据规模有些后怕,后来想到long long 可以达 ...
随机推荐
- 打造百度网盘备份利器:自动备份Linux VPS文件和多线程下载百度网盘资源
前一段时间国内的各大网盘百度云盘,金山快盘,360云盘,华为网盘为争夺用户上演空间容量博弈,网盘商们还固执地以为中国的网民都不懂网络技术,可以像某公司那样用一些数字的手段来忽悠用户,参与到网盘商的数字 ...
- WPF的UI虚拟化
许多时候,我们的界面上会呈现大量的数据,如包含数千条记录的表格或包含数百张照片的相册.由于呈现UI是一件开销比较大的动作,一次性呈现数百张照片就目前的电脑性能来说是需要占用大量内存和时间的.因此需要对 ...
- arcgis andriod 点击后变色
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- Android内存优化6 了解Android是如何管理App内存
1, Dalvik & ART Android在4.4之前一直使用的Dalvik虚拟机作为App的运行VM的, 4.4中引入了ART作为开发者备选, 5.0起正式将ART作为默认VM了. 我们 ...
- fiddler抓取手机上https数据配置和失败的解决办法
1. 设置fiddler,Tools-Options... 抓取https的话,勾选红框中的内容 2. fiddler默认监听端口8888 3. 查看本机IP 4. 打开手机 设置-无线局域 ...
- iOS:操作队列实现多线程NSOperation
NSOperation具体使用:直接继承NSObject 它的子类有:NSBlockOperation.NSInvocationOperation 还有一个必须的类,队列,用来装创建的线程 NSOpe ...
- Android Studio断点调试
Android Studio断点调试 Android Studio包含一个debugger程序,可以帮助你在模拟器和真机上调试你的android应用.通过Android Studio的debugger ...
- android 步骤控件的使用
有的时候我们做Android开发会用到表示步骤的需求.这时候github给我们提供了一个非常好地表示步骤的组件,使用她仅仅须要4步就能够完毕了. 项目地址https://github.com/anto ...
- MySQL SELECT 语句
SELECT语句: products表例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1eWluZ18xMDAx/font/5a6L5L2T ...
- nginx安装说明
下载地址:http://nginx.org/en/download.html 安装版本:1.10.0 安装配置如下: /etc/nginx 目录 /home/nginx目录 --prefix=/hom ...