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 ...
随机推荐
- EF之贪婪加载和延迟加载
这篇文章将讨论查询结果的控制 在使用EF(Entity Framework)的过程中,很多时候我们会进行查询的操作,因此知道哪些数据会被加载到内存当中就至关重要.在多多的情况下,你可能并并不需要加载全 ...
- CS231n 2016 通关 第四章-NN 作业
cell 1 显示设置初始化 # A bit of setup import numpy as np import matplotlib.pyplot as plt from cs231n.class ...
- 2014年将会受欢迎的IT技能--你有多少哪?
据国外媒体报道,据Global Knowledge等十几家研究机构发布的2014年IT技能和薪金调查报告显示,2014年最受欢迎的十大IT技能如下: 1.编程与应用开发 据美国劳工统计局称,开发者和程 ...
- centos时区
执行:cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime是将shanghai时区设置为系统时区 执行:date +%s 获取的是系统的utc时间戳 ...
- python_re函数
1,贪婪和非贪婪模式 重复运算符默认是贪婪的,即会进行尽可能多的匹配 代码示例: >>> import re >>> emphasis_pattern = re.c ...
- Weekly Contest 78-------->809. Expressive Words (stack)
Sometimes people repeat letters to represent extra feeling, such as "hello" -> "he ...
- Go语言之父谈Go:大道至简
http://www.csdn.net/article/2012-07-05/2807113-less-is-exponentially-more 摘要:导读:这篇文章是Google首席工程师.Go语 ...
- SpringBoot2.0 整合 RocketMQ ,实现请求异步处理
一.RocketMQ 1.架构图片 2.角色分类 (1).Broker RocketMQ 的核心,接收 Producer 发过来的消息.处理 Consumer 的消费消息请求.消息的持 久化存储.服务 ...
- linux 系统运行级别(转)
Linux系统有7个运行级别(runlevel)运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆运行级别2:多 ...
- E: 软件包 ffmpeg 没有可供安装的候选者
问题:在DSO安装依赖项ffmpeg时遇到“E: 软件包 ffmpeg 没有可供安装的候选者”这一问题. 解决:在Ubuntu上gstreamer0.10-ffmpeg属于额外的版权受限程序,gstr ...