传送门

一群蚂蚁能被吃,也就是走到指定边的两端点之一要走到另一端点时有\(k\)只,我们可以从这两端点逆推,记两个值为走到某个点时最后会被吃掉\(k\)只蚂蚁的蚂蚁数量范围,式子下面有,很好理解(雾).最后在每个叶子节点二分查找有多少个数在区间内即可

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register
#define inf 2099999999
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define db double
#define eps (1e-5) using namespace std;
const int N=1000000+10;
il LL rd()
{
re LL x=0,w=1;re char ch=0;
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
LL d[N];
int to[N<<1],nt[N<<1],hd[N],tot=1;
il void add(int x,int y)
{
++tot,to[tot]=y,nt[tot]=hd[x],hd[x]=tot,++d[x];
++tot,to[tot]=x,nt[tot]=hd[y],hd[y]=tot,++d[y];
}
int n,g,k,ma,xx,yy;
LL ll[N],rr[N],ans,a[N];
void dfs(int x,int ffa)
{
for(int i=hd[x];i;i=nt[i])
{
int y=to[i];
if(y==ffa) continue;
ll[y]=min(ll[x]*max(d[x]-1,1ll),a[g]+10),rr[y]=min((rr[x]+1)*max(d[x]-1,1ll)-1,a[g]+10);
dfs(y,x);
}
if(d[x]==1)
{
int l=0,r=g,z=g,zz=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(a[mid]<ll[x]) z=mid,l=mid+1;
else r=mid-1;
}
l=0,r=g;
while(l<=r)
{
int mid=(l+r)>>1;
if(a[mid]<=rr[x]) zz=mid,l=mid+1;
else r=mid-1;
}
ans+=max(zz-z,0);
}
} int main()
{
n=rd(),g=rd(),k=rd();
for(int i=1;i<=g;i++) a[i]=rd();
sort(a+1,a+g+1);
a[0]=-1;
add(xx=rd(),yy=rd());
for(int i=2;i<n;i++) add(rd(),rd());
ll[xx]=rr[xx]=k,dfs(xx,yy);
ll[yy]=rr[yy]=k,dfs(yy,xx);
printf("%lld\n",ans*k);
return 0;
}

luogu P3576 [POI2014]MRO-Ant colony的更多相关文章

  1. 洛谷——P3576 [POI2014]MRO-Ant colony

    P3576 [POI2014]MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. ...

  2. 洛谷 P3576 [POI2014]MRO-Ant colony

    P3576 [POI2014]MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. ...

  3. [BZOJ3872][Poi2014]Ant colony

    [BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...

  4. bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分

    3872: [Poi2014]Ant colony Time Limit: 30 Sec  Memory Limit: 128 MB Description   There is an entranc ...

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

    [BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...

  6. Codeforces 474 F. Ant colony

    线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...

  7. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. 【BZOJ3872】Ant colony(二分,动态规划)

    [BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...

  9. CodeForces 474F Ant colony ST+二分

    Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问 ...

随机推荐

  1. HTMl Dom操作

    訪問: getElementById():帶回指定id的所有元素 getElementByTagName():帶回指定標籤名的所有的元素 getElementByClassName():帶回指定類型名 ...

  2. timescale 时间尺度

    1 `timescale为模块指定参考时间单位 `timescale<reference_time_unit>/<time_precision> 2 module endmou ...

  3. BZOJ2159 Crash的文明世界(树形dp+斯特林数)

    根据组合意义,有nk=ΣC(n,i)*i!*S(k,i) (i=0~k),即将k个有标号球放进n个有标号盒子的方案数=在n个盒子中选i个将k个有标号球放入并且每个盒子至少有一个球. 回到本题,可以令f ...

  4. Cookie 类

    最近项目中用到cookie,所以就写了个cookie帮助类 public class CookHelper { HttpResponseBase Response = null; HttpReques ...

  5. 机器学习工作流程第一步:如何用Python做数据准备?

    这篇的内容是一系列针对在Python中从零开始运用机器学习能力工作流的辅导第一部分,覆盖了从小组开始的算法编程和其他相关工具.最终会成为一套手工制成的机器语言工作包.这次的内容会首先从数据准备开始. ...

  6. Ubuntu/Unity中更改窗口修饰键Alt为Super

    在Ubuntu中的Unity桌面环境里,可以使用Alt配合鼠标左键拖动窗口,这一方便的设定有许多不方便的地方.和很多的软件有热键上的冲突,比如Visual Stdio Code的多光标控制功能. 注意 ...

  7. springboot集成druid连接池

    使用druid连接池主要有几步: 1.添加jar和依赖 <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...

  8. Qt shortcuts

    ESC 切换到代码编辑模式 F1   查看帮助 F4    .h 与 .c or .cpp之间切换 shift + F2 声明与定义间切换 Ctrl + tab 切换已打开的文件 Ctrl + B 编 ...

  9. 谨记:new Date()在IOS中的坑

    第一次遇到,记录一下 要创建一个指定时间的new Date对象时,通常的做法是: new Date("2018-04-27 11:11");然后就可以根据这个获取年月日等信息 这种 ...

  10. textarea 字体限制,超出部分不显示并及时显示还剩字体个数

    1)HTML <textarea class="box" ></textarea > 2)JQ: $(function(){ $(".box&qu ...