【BZOJ3872】[Poi2014]Ant colony 树形DP+二分
【BZOJ3872】[Poi2014]Ant colony
Description

Input
Output
Sample Input
3 4 1 9 11
1 2
1 4
4 3
4 5
4 6
6 7
Sample Output
题解:可以先把第一条边拆掉,然后分成两棵树进行树形DP。因为根节点的取值是确定的,并且每个点的度数也是确定的,所以可以根据父亲的取值范围得出儿子的取值范围,最终得出所有叶子节点的取值范围。然后将m数组排序,对于每个节点都在m数组里二分一下统计答案即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1000010;
const ll inf=1<<30;
int n,m,cnt,r1,r2;
ll K,ans;
int to[maxn<<1],next[maxn<<1],head[maxn],d[maxn],fa[maxn];
ll l[maxn],r[maxn],v[maxn];
inline void add(int a,int b)
{
to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
void dfs(int x)
{
for(int i=head[x];i!=-1;i=next[i]) if(to[i]!=fa[x])
{
fa[to[i]]=x;
if(d[to[i]]==1) l[to[i]]=l[x],r[to[i]]=r[x];
else l[to[i]]=min(inf,l[x]*(d[to[i]]-1)),r[to[i]]=min(inf,(r[x]+1)*(d[to[i]]-1)-1);
dfs(to[i]);
}
}
int main()
{
n=rd(),m=rd(),K=rd();
int i,a,b;
for(i=1;i<=m;i++) v[i]=rd();
v[m+1]=inf+1;
sort(v+1,v+m+1);
r1=rd(),r2=rd(),d[r1]++,d[r2]++;
memset(head,-1,sizeof(head));
for(i=1;i<n-1;i++) a=rd(),b=rd(),add(a,b),add(b,a),d[a]++,d[b]++;
if(d[r1]==1) l[r1]=r[r1]=K;
else l[r1]=K*(d[r1]-1),r[r1]=(K+1)*(d[r1]-1)-1;
if(d[r2]==1) l[r2]=r[r2]=K;
else l[r2]=K*(d[r2]-1),r[r2]=(K+1)*(d[r2]-1)-1;
dfs(r1),dfs(r2);
for(i=1;i<=n;i++) if(d[i]==1)
a=lower_bound(v+1,v+m+1,l[i])-v,b=upper_bound(v+1,v+m+1,r[i])-v,ans+=(b-a)*K;
printf("%lld",ans);
return 0;
}
【BZOJ3872】[Poi2014]Ant colony 树形DP+二分的更多相关文章
- bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分
3872: [Poi2014]Ant colony Time Limit: 30 Sec Memory Limit: 128 MB Description There is an entranc ...
- [bzoj3872][Poi2014]Ant colony_树形dp
Ant colony bzoj-3872 Poi-2014 题目大意:说不明白.....题目链接 注释:略. 想法:两个思路都行. 反正我们就是要求出每个叶子节点到根节点的每个路径权值积. 可以将边做 ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- $bzoj3872\ [Poi2014]\ Ant\ colony$ 二分+$dp$
正解:二分+$dp$ 解题报告: 传送门$QwQ$ 一年过去了依然没有头绪,,,$gql$的$NOIp$必将惨败了$kk$. 考虑倒推,因为知道知道除数和答案,所以可以推出被除数的范围,然后一路推到叶 ...
- 【BZOJ3872】Ant colony(二分,动态规划)
[BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...
- 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...
- hdu 3586 Information Disturbing(树形dp + 二分)
本文出自 http://blog.csdn.net/shuangde800 题目链接: hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...
- BZOJ3420[POI2013]Triumphal arch&BZOJ5174[Jsoi2013]哈利波特与死亡圣器——树形DP+二分答案
题目大意: 给一颗树,1号节点已经被染黑,其余是白的,两个人轮流操作,一开始B在1号节点,A选择k个点染黑,然后B走一步,如果B能走到A没染的节点则B胜,否则当A染完全部的点时,A胜.求能让A获胜的最 ...
- hdu 3586 树形dp+二分
题目大意:给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵 树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线.现要切断前线和司令部的联系,每次切断边的费用不能超过上限lim ...
随机推荐
- Python 实现小数和百分数的相互转换
# -*- coding: utf-8 -*- #百分比转换位小数 # -*- coding: utf-8 -*- s = '20%' # 默认要转换的百分比是字符串aa = float(s.stri ...
- struts2实现简单文件上传
struts2 在内部已经帮我们做了很多封装的工作,我们只需要简单的写一些配置即可. 1 表单页面 <form action="${pageContext.request.contex ...
- zoj 3299(区间改动+离散化)
题意:有n个由小木块组成的长条木块要掉下来.给出木块的左右区间,然后有给了m个木板的左右区间和高度用来接住木块,由于木块是由小木块接触组成的,也就是木板能够接住一部分的木块.剩下的会继续掉落,问最后每 ...
- 宏里面的(void)0
在<c标准库>实现assert.h中有一个语句: #define assert(test) ((test)?(void)0 : _Assert(__FILE__":"_ ...
- 【Android】14.2 外部文件存储和读取
分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 1.基本概念 内部存储的私有可用存储空间一般都不会很大,对于容量比较大的文件,例如视频等,应该将其存储在外部存储设 ...
- rip中的连续子网以及不连续子网
RIPv1 RIPv2 距离矢量2 距离矢量 最大跳计数15 最大跳计数15 有类的 无类的 基于广播的 基于组播224.0.09 不支持VLSM 支持VLSM 无认证 允许MD5认证 不支持不 ...
- ssm开发系的统架构图
- Python 内置模块函数filter reduce
1.filter()实现过滤的功能 2.reduce()对序列中元素的连续操作可以通过循环来处理 3.map()对tuple元组进行解包操作,调用时设置map()的第一个参数为None 4.使用red ...
- java 代理模式,观察者模式
代理模式1 import <a href="http://lib.csdn.net/base/17" class='replace_word' title="Jav ...
- array2json
原文:jQuery方法扩展:type, toJSON, evalJSON. http://zhkac.iteye.com/blog/499330 .2013-05-19 (function($) { ...