poj 3140 Contestants Division [DFS]
题意:一棵树每个结点上都有值,现删掉一条边,使得到的两棵树上的数值和差值最小。
思路:这个题我直接dfs做的,不知道树状dp是什么思路。。一开始看到数据规模有些后怕,后来想到long long 可以达到10^18,我突然就释然了。
整体思路就是,先记录下整棵树的数值之和tot,然后对这棵树进行一遍dfs,每个结点都维护一个num值,num[x]表示结点x和它子树上的数值和。每求出一个结点的num值,就计算下tot - num[x]和num[x]的差值。dfs结束后最小的差值即为结果。
另外,要注意的一点是,如果用res来存储最后结果,初始化应为无穷大,而因为它是long long类型,可以初始化为0x3f3f3f3f3f3f3f3f。如果你还是按照int的大小来初始化为无穷大,会发现wa,就是这么回事。。。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define maxn 100005
#define maxp 1000005
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std; struct node
{
int v, next;
}edge[maxp];
int num_edge, head[maxn];
void init_edge()
{
num_edge = ;
memset(head, -, sizeof(head));
}
void addedge(int a,int b)
{
edge[num_edge].v = b;
edge[num_edge].next = head[a];
head[a] = num_edge++;
} long long dif(long long a,long long b)
{
return (a > b) ? a - b : b - a;
} int stu[maxn], n, m;
bool vis[maxn];
long long num[maxn], tot, res;
void dfs(int x)
{
num[x] = stu[x];
vis[x] = ;
for (int i = head[x]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if (vis[v]) continue;
dfs(v);
num[x] += num[v];
}
res = min(res, dif(num[x], tot - num[x]));
}
int main()
{
int cas = ;
while (~scanf("%d%d", &n, &m) && n && m)
{
tot = ;
for (int i = ; i <= n; i++)
{
scanf("%d",&stu[i]);
tot += stu[i];
}
init_edge();
memset(vis, , sizeof(vis));
for (int i = ; i <= m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
addedge(a, b);
addedge(b, a);
}
res = inf;
dfs();
printf("Case %d: %lld\n", cas++, res);
}
return ;
}
poj 3140 Contestants Division [DFS]的更多相关文章
- 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? dfs计数+枚举)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- 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】
<题目链接> 题目大意:给你一棵树,让你找一条边,使得该边的两个端点所对应的两颗子树权值和相差最小,求最小的权值差. 解题分析: 比较基础的树形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 3104 Contestants Division
Contestants Division Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10597 Accepted: ...
随机推荐
- 图学java基础篇之集合
(本文部分图片引用自其他博客,最后有链接,侵删.由于笔记使用markdown记录,格式可能不是太好看,见谅) 集合结构 红字为java.util包下的,绿字为concurrent包下扩展的与并发相关的 ...
- mysql查询当天的数据
mysql查询当天的数据 贴代码: #两个时间都使用to_days()函数 select * from reple where to_days(create_time) = to_days(NOW() ...
- 解决pymysql不能实时查询最新的数据
#在网上查询到的原因为: InnoDB 的默认隔离级别.它可以防止任何被查询的行被其他事务更改,从而阻止不可重复的读取,而不是 幻读取.它使用中度严格的锁定策略,以便事务内的所有查询都会查看同一快照中 ...
- Python+Selenium练习篇之8-利用css定位元素
前面介绍了,XPath, id , class , link text, partial link text, tag name, name 七大元素定位方法,本文介绍webdriver支持的最后一个 ...
- Python+Selenium基础篇之2-打开和关闭火狐浏览器
本节介绍如何初始化一个webdriver实例对象driver,然后打开和关闭firefox浏览器.要用selenium打开fiefox浏览器.首先需要去下载一个driver插件geckodriver. ...
- hadoop配置文件: hdfs-site.xml, mapred-site.xml
dfs.name.dir Determines where on the local filesystem the DFS name node should store the name table( ...
- Block Nested-Loop 和 Batched Key Access
官方文档:https://dev.mysql.com/doc/refman/5.7/en/bnl-bka-optimization.html BNL和BKA是MySQL 表关联的两种关联算法 比如t1 ...
- linux下telnet安装与使用
现在管理linux基本都用crt.xshell或者putty,已经没什么人用telnet,因为后续需要讲zabbix免客户端监控只telnet,通过telnet来监控服务器性能. yum安装telne ...
- HDU6438 Buy and Resell 解题报告(一个有趣的贪心问题的严格证明)
写在前面 此题是一个很容易想到的贪心题目,但是正确性的证明是非常复杂的.然而,目前网上所有题解并未给出本题贪心算法的任何正确性证明,全部仅停留在描述出一个贪心算法.本着对算法与计算机科学的热爱(逃), ...
- linux编程学习
linux编程学习 工具篇 “公欲善其事,必先利其器”.编程是一门实践性很强的工作,在你以后的学习或工作中,你将常常会与以下工具打交道, 下面列出学习 C 语言编程常常用到的软件和工具. (一)操作系 ...