POJ 3140 Contestants Division (树形DP,简单)
题意:
有n个城市,构成一棵树,每个城市有v个人,要求断开树上的一条边,使得两个连通分量中的人数之差最小。问差的绝对值。(注意本题的M是没有用的,因为所给的必定是一棵树,边数M必定是n-1)
思路:
考虑当前节点t,当断开t与父亲的边时,“子树t中的人数”与“剩下的人数”之差的绝对值若最小,则为答案。
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <deque>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define abs(x) ((x)<0?-(x):(x))
#define INF 2147483647
#define LL long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
int edge_cnt, head[N], w[N];
LL ans, sum;
struct node
{
int from, to, next;
node(){};
node(int from,int to,int next):from(from),to(to),next(next){};
}edge[N*]; void add_node(int from,int to)
{
edge[edge_cnt]=node(from,to,head[from]);
head[from]=edge_cnt++;
} LL DFS(int t,int far) //枚举删除t头上的边
{
node e;
LL cnt=;
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if(e.to==far) continue;
cnt+=DFS(e.to, t);
}
cnt+=w[t]; //本子树的人数
ans=min(ans, abs( *cnt-sum ));
return cnt;
} int main()
{
//freopen("input.txt", "r", stdin);
int a, b, n, m, Case=;
while(scanf("%d%d",&n,&m), n+m)
{
sum=edge_cnt=;
memset(head,-,sizeof(head)); for(int i=; i<=n; i++)
{
scanf("%d",&w[i]);
sum+=w[i];
}
ans=sum;
for(int i=; i<m; i++)
{
scanf("%d%d",&a,&b);
add_node(a,b);
add_node(b,a);
}
DFS(,-);
printf("Case %d: %lld\n", ++Case, ans);
}
return ;
}
AC代码
POJ 3140 Contestants Division (树形DP,简单)的更多相关文章
- 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 基础树形dp
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10704 Accepted: ...
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- POJ 3140 Contestants Division 【树形DP】
<题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形DP. #include <cstdi ...
- POJ 3140 Contestants Division
题目链接 题意很扯,就是给一棵树,每个结点有个值,然后把图劈成两半,差值最小,反正各种扯. 2B错误,导致WA了多次,无向图,建图搞成了有向了.... #include <cstdio> ...
- poj 3140 Contestants Division [DFS]
题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小. 思路:这个题我直接dfs做的,不知道树状dp是什么思路..一开始看到数据规模有些后怕,后来想到long long 可以达 ...
- [poj3140]Contestants Division树形dp
题意:切掉树上的某条边,使分开的两棵树上各点的权值和差值最小. 与hdu2196不同的是,此题是点权,其他无太大差别,注意数据范围. 先求出每个节点的子树权值和,然后自底向上dp即可.取$\min ( ...
- POJ 2378 Tree Cutting 3140 Contestants Division (简单树形dp)
POJ 2378 Tree Cutting:题意 求删除哪些单点后产生的森林中的每一棵树的大小都小于等于原树大小的一半 #include<cstdio> #include<cstri ...
随机推荐
- Word2013 在一个页面双列显示
1. 效果图 2. 实现方法 (1) 进入页面布局 (2) 选中要整理的字,选中Columns,然后选择Two
- Ubuntu无法访问Windows磁盘, 且无提示信息
现象描述, 双系统Ubuntu和Windows, 进入Ubuntu后无法访问Windows盘内容, 且图标闪烁无任何错误信息提示. 1.安装ntfsfix sudo apt-get install n ...
- <正则吃饺子>:关于集合的简单整理总结
项目中用到的集合不可谓不多,对于自己的一次面试,要求说下自己用过的集合,自己开始说的并不系统也不完整,一直耿耿于怀,特整理一下,以备后期之用和帮助后来者. package com.love.malin ...
- 后端开发-Mybatis开发之一
2018-12-29 Mybatis简单开发:我是在eclipse上进行Java项目开发 目录结构如下: 内容如下: mysql.sql: 用于创建测试用的数据库以及表: 可以直接导入mys ...
- CF-798A
A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- eclispe的使用
1.eclipse 有的时候,子类包都已经写了,这时候要添加一个父类包,如果在包结构下是添加不了的,这时候需要切换格式:
- PPJQR-GKK-2深度学习及应用
参考资料: UFLDL:http://deeplearning.stanford.edu/wiki/index.php/UFLDL_Tutorial Deep Learning Turtorial:h ...
- [WIP]express 入门
创建: 2019/04/10 install 创建并移动进新文件夹 mkdir sample_app cd sample_app 创建package.json并导入express npm ini ...
- Maven使用阿里云公共仓库
https://help.aliyun.com/document_detail/102512.html?spm=a2c40.aliyun_maven_repo.0.0.3618305449xZaK
- Lightoj1037【状压DP】
题意: 给出n个怪的生命值,然后n个怪手里有一把枪,给出n*n的矩阵代表第i个怪对第j个怪的伤害值: 现在让你去干掉n个怪,只能平A使怪扣一滴血,干掉目标后, 可以把这个目标的武器拿进口袋然后用这个武 ...