Luogu3576 POI2014 MRO-Ant colony 【树形DP】*
Luogu3576 POI2014 MRO-Ant colony
The ants are scavenging an abandoned ant hill in search of food.
The ant hill has nn chambers and n-1n−1 corridors connecting them.
We know that each chamber can be reached via a unique path from every other chamber.
In other words, the chambers and the corridors form a tree.
There is an entrance to the ant hill in every chamber with only one corridor leading into (or out of) it.
At each entry, there are gg groups ofm1,m2,⋯,mgm1,m2,⋯,mgants respectively.
These groups will enter the ant hill one after another, each successive group entering once there are no ants inside.
Inside the hill, the ants explore it in the following way:
Upon entering a chamber with dd outgoing corridors yet unexplored by the group,the group divides into dd groups of equal size. Each newly created group follows one of the d corridors.If d=0 , then the group exits the ant hill.
If the ants cannot divide into equal groups, then the stronger ants eat the weaker until a perfect division is possible.Note that such a division is always possible since eventually the number of ants drops down to zero.Nothing can stop the ants from allowing divisibility - in particular, an ant can eat itself, and the last one remaining will do so if the group is smaller than dd .
The following figure depicts mm ants upon entering a chamber with three outgoing unexplored corridors, dividing themselves into three (equal) groups of ⌊m/3⌋⌊m/3⌋ ants each.
A hungry anteater dug into one of the corridors and can now eat all the ants passing through it.
However, just like the ants, the anteater is very picky when it comes to numbers.
It will devour a passing group if and only if it consists of exactly k ants.
We want to know how many ants the anteater will eat.
给一棵树,对于每个叶子节点,都有g群蚂蚁要从外面进来,每群蚂蚁在行进过程中只要碰到岔路,就将平均地分成岔路口数-1那么多份,然后平均地走向剩下的那些岔路口,余下的蚂蚁自动消失,树上有一个关键边,假如有一群蚂蚁通过了这条边且数量恰好为k,这k只蚂蚁就被吃掉,问一共有多少只蚂蚁被吃掉
输入输出格式
输入格式:
The first line of the standard input contains three integers n, g , k (2≤n,g≤n,1≤k≤109)(2≤n,g≤n,1≤k≤109), separated by single spaces.These specify the number of chambers, the number of ant groups and the number of ants the anteater devours at once. The chambers are numbered from 1 to n.
The second line contains g integers m1,m2,⋯,mgm1,m2,⋯,mg( 1≤mi≤1091≤mi≤109), separated by single spaces, where mimigives the number of ants in the i -th group at every entrance to the ant hill. The n-1 lines that follow describe the corridors within the ant hill;the i -th such line contains two integers ai,biai,bi ( 1≤ai,bi≤n1≤ai,bi≤n ), separated by a single space, that indicate that the chambers no. aiai and bibi are linked by a corridor. The anteater has dug into the corridor that appears first on input.
输出格式:
Your program should print to the standard output a single line containing a single integer: the number of ants eaten by the anteater.
我们如果把那条需要计算贡献的边断开
就变成了两个子树,非常的友好, 我们发现询问和询问之间是独立的,所以我们可以考虑把每个点所有讯问的贡献一起统计
这样我们只需要DP出每个点合法的最大权值和最小权值,然后二分查找一下就好了
挂在二分边界上,wuwuwuwu
#include<bits/stdc++.h>
using namespace std;
#define N 1000010
#define LL long long
inline LL read(){
LL res=0,w=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(isdigit(ch))res=(res<<3)+(res<<1)+ch-'0',ch=getchar();
return w*res;
}
struct Edge{LL next,v;}E[N<<1];
LL S,T,cnt=0,k,n,m,head[N],d[N];
LL maxv[N],minv[N],num[N],ans,maxq=0;
vector<int> g;
void add(LL u,LL v){
E[++cnt]=(Edge){head[u],v};head[u]=cnt;
E[++cnt]=(Edge){head[v],u};head[v]=cnt;
d[u]++;d[v]++;
}
void dfs(LL u,LL fa){
for(int i=head[u];i;i=E[i].next){
int v=E[i].v;
if(v==fa)continue;
maxv[v]=(maxv[u]+1)*(d[u]-1)-1;
maxv[v]=min(maxq,maxv[v]);
minv[v]=minv[u]*(d[u]-1);
if(minv[v]<=maxq)dfs(v,u);
}
}
LL check(LL vl){
int l=1,r=m,ans=m+1;//注意二分边界
while(l<=r){
int mid=(l+r)>>1;
if(num[mid]>vl)ans=mid,r=mid-1;
else l=mid+1;
}
return ans;
}
int main(){
n=read();m=read();k=read();
for(int i=1;i<=m;i++)num[i]=read(),maxq=max(maxq,num[i]);
sort(num+1,num+m+1);
for(int i=1;i<n;i++){
int u=read(),v=read();
if(i==1)S=u,T=v;
add(u,v);
}
for(int i=1;i<=n;i++)if(d[i]==1)g.push_back(i);
maxv[S]=minv[S]=k;
maxv[T]=minv[T]=k;
dfs(S,T);
dfs(T,S);
for(int i=0;i<g.size();i++)
ans+=check(maxv[g[i]])-check(minv[g[i]]-1);
printf("%lld\n",1ll*ans*k);
}
Luogu3576 POI2014 MRO-Ant colony 【树形DP】*的更多相关文章
- 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分
[BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...
- 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$ 二分+$dp$
正解:二分+$dp$ 解题报告: 传送门$QwQ$ 一年过去了依然没有头绪,,,$gql$的$NOIp$必将惨败了$kk$. 考虑倒推,因为知道知道除数和答案,所以可以推出被除数的范围,然后一路推到叶 ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- 【BZOJ3522】[Poi2014]Hotel 树形DP
[BZOJ3522][Poi2014]Hotel Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房 ...
- 【BZOJ3522】【BZOJ4543】【POI2014】Hotel 树形DP 长链剖分 启发式合并
题目大意 给你一棵树,求有多少个组点满足\(x\neq y,x\neq z,y\neq z,dist_{x,y}=dist_{x,z}=dist_{y,z}\) \(1\leq n\leq 1 ...
- BZOJ3522[Poi2014]Hotel——树形DP
题目描述 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽 ...
- bzoj 3829: [Poi2014]FarmCraft 树形dp+贪心
题意: $mhy$ 住在一棵有 $n$ 个点的树的 $1$ 号结点上,每个结点上都有一个妹子. $mhy$ 从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装 $zhx$ 牌杀毒 ...
随机推荐
- Mysql数据库实用语句集
mysql实用语句 (1).从第0行开始取,取3行(一般用于分页)select * from student limit 0,3 (2).查看当前数据库编码 show variables like ' ...
- 解决虚拟机安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题
环境说明:系统:Windows 8.1 简体中文专业版 虚拟机:VMware Workstation 11.0.0 报错:此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态.如 ...
- 异步提交表单插件jquery.form.min.js的使用实例
因为项目中需要达到效果:前台点击按钮弹出文件选择框,选择文件确定之后,上传到后台对文件进行处理并给出响应信息. 尝试过使用$.post,$.ajsx,将表单序列化之后传到后台,但是后台并不能收到文件, ...
- JSP 标准标签库(JSTL)
JSP 标准标签库(JSTL) JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. JSTL支持通用的.结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签, ...
- 设计模式--责任链模式C++实现
责任链模式C++实现 1定义 使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系.将这些对象链成一条链,并沿着这条链传递该请求/命令,直到有对象处理它为止 注:这里的请求.命令正 ...
- 二十七 Python分布式爬虫打造搜索引擎Scrapy精讲—通过自定义中间件全局随机更换代理IP
设置代理ip只需要,自定义一个中间件,重写process_request方法, request.meta['proxy'] = "http://185.82.203.146:1080&quo ...
- Ubuntu 下Python 环境问题
问题描述: 原先使用Anaconda环境,若卸载后仍不能恢复到系统默认的Python环境. 解决方案: shell 寻找缓存路径,python的扩展/home/tom/anaconda/bin/pyt ...
- OpenStack Mitaka Neutron SR-IOV配置
### 一.在所有节点(控制节点.计算节点) 1.修改BIOS ``` BOIS里面开启SR-IOV功能 开启 VT-d (inter virtualization technology)和 SR-I ...
- pycharm debug
1 debug 的模式,只有在打断点是才能运行. 2 主方法运行的时候 ,可以在下面输入一行 a =1 .方便debug 打完断电的会出现三角头, 运行甲壳虫 3 一步一步运行, F8 , 当 ...
- session、cookie、viewstate
session的用法 定义:保存在服务器内存的数据,sesson 只应该应用在需要跨页面且与每个访问用户相关的变量和对象存储上,session在默认情况下20分钟就过期,在页面之中最好不要过多使用,因 ...