题目大意:
  给定一棵$n(n\le10^6)$个结点的树。在每个叶子结点,有$g$群蚂蚁要从外面进来,其中第$i$群有$m_i$只蚂蚁。这些蚂蚁依次爬树(一群蚂蚁爬完后才会爬另一群),若当前经过结点度为$d+1$,蚂蚁数量为$m$,则接下来没走过的$d$个方向,每个方向爬$\lfloor\frac md\rfloor$只蚂蚁,剩下蚂蚁消失。在给定的一条边上有一只食蚁兽,若当前经过这条边的蚂蚁数量为$k$,则吃掉这些蚂蚁。问最后能吃到多少蚂蚁?

思路:
  从给定边出发BFS,算出每条边会被吃掉的蚁群规模的上界和下界。若到了叶子节点,就在$\{m_i\}$中二分。然后洛谷和SZKOpuł上都能随便AC,BZOJ大力卡一波常才过。

 #include<cstdio>
#include<algorithm>
#include<sys/mman.h>
#include<sys/stat.h>
typedef long long int64;
class BufferedInputStream {
private:
char *buf,*p;
int size;
public:
BufferedInputStream() {
register int fd=fileno(stdin);
struct stat sb;
fstat(fd,&sb);
size=sb.st_size;
p=buf=reinterpret_cast<char*>(mmap(,size,PROT_READ,MAP_PRIVATE,fileno(stdin),));
}
char getchar() {
return (p==buf+size||*p==EOF)?EOF:*p++;
}
};
BufferedInputStream in;
inline int getint() {
register char ch;
while(!__builtin_isdigit(ch=in.getchar()));
register int x=ch^'';
while(__builtin_isdigit(ch=in.getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=1e6+;
bool vis[N];
int deg[N],q[N],head,tail,h[N],sz;
int64 m[N],max[N],min[N];
struct Edge {
int to,next;
};
Edge e[N<<];
inline void add_edge(const int &u,const int &v) {
e[++sz]=(Edge){v,h[u]};h[u]=sz;deg[u]++;
e[++sz]=(Edge){u,h[v]};h[v]=sz;deg[v]++;
}
int main() {
const int n=getint(),g=getint(),k=getint();
int64 lim=;
for(register int i=;i<=g;i++) {
lim=std::max(lim,m[i]=getint());
}
std::sort(&m[],&m[g]+);
for(register int i=;i<n;i++) {
const int u=getint(),v=getint();
if(i==) {
max[u]=min[u]=max[v]=min[v]=k;
vis[u]=vis[v]=true;
q[tail++]=u;
q[tail++]=v;
}
add_edge(u,v);
}
int64 ans=;
while(head<tail) {
const int &x=q[head++];
if(deg[x]==) {
ans+=(int64)(std::upper_bound(&m[],&m[g]+,max[x])-std::lower_bound(&m[],&m[g]+,min[x]))*k;
}
for(register int i=h[x];i;i=e[i].next) {
const int &y=e[i].to;
if(vis[y]) continue;
max[y]=std::min((max[x]+)*(deg[x]-)-,lim);
min[y]=min[x]*(deg[x]-);
vis[y]=true;
if(min[y]<=lim) q[tail++]=y;
}
}
__builtin_printf("%lld\n",ans);
return ;
}

[POI2014]Ant colony的更多相关文章

  1. [BZOJ3872][Poi2014]Ant colony

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

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

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

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

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

  4. bzoj 3872 [Poi2014]Ant colony——二分答案

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 可以倒推出每个叶子节点可以接受的值域.然后每个叶子二分有多少个区间符合即可. 注意一开 ...

  5. bzoj 3872: [Poi2014]Ant colony【树形dp+二分】

    啊我把分子分母混了WA了好几次-- 就是从食蚁兽在的边段成两棵树,然后dp下去可取的蚂蚁数量区间,也就是每次转移是l[e[i].to]=l[u](d[u]-1),r[e[i].to]=(r[u]+1) ...

  6. $bzoj3872\ [Poi2014]\ Ant\ colony$ 二分+$dp$

    正解:二分+$dp$ 解题报告: 传送门$QwQ$ 一年过去了依然没有头绪,,,$gql$的$NOIp$必将惨败了$kk$. 考虑倒推,因为知道知道除数和答案,所以可以推出被除数的范围,然后一路推到叶 ...

  7. Luogu3576 POI2014 MRO-Ant colony 【树形DP】*

    Luogu3576 POI2014 MRO-Ant colony The ants are scavenging an abandoned ant hill in search of food. Th ...

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

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

  9. [bzoj3872][Poi2014]Ant colony_树形dp

    Ant colony bzoj-3872 Poi-2014 题目大意:说不明白.....题目链接 注释:略. 想法:两个思路都行. 反正我们就是要求出每个叶子节点到根节点的每个路径权值积. 可以将边做 ...

随机推荐

  1. Oracle 学习----游标(使用无参光标cursor)

    表: ----游标----------------------------- declare cursor tt is select name,sal from temp; vname temp.na ...

  2. [转]LVS+Keepalived负载均衡配置

    简介 来源:https://www.cnblogs.com/MacoLee/p/5858995.html lvs一般是和keepalived一起组合使用的,虽然也可以单独使用lvs,但配置比较繁琐,且 ...

  3. 2 25urllib.py

    """ urllib.request.urlopen(url,data,timeout) """ # from urllib.request ...

  4. Leetcode 665.非递减数列

    非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...

  5. 聊聊、Mybatis XML

    引入相应的依赖包 <dependency><groupId>org.mybatis</groupId><artifactId>mybatis-sprin ...

  6. XPS Enable GPIO on EMIO interface 不见了

    按照 <嵌入式系统软硬件协同设计实战指南 -- 基于xilinx Zynq>第九章 zedboard 入门 我一步一步做到9.1.2 (13) 发现 没有  Enable GPOI on ...

  7. atom插件之less-autocompile

    less-autocompile package Auto compile LESS file on save. Add the parameters on the first line of the ...

  8. jQuery选择器之id选择器

    页面的任何操作都需要节点的支撑,开发者如果快速高效的找到指定的节点也是前端开发中的一个重点.jQuery提供了一系列的选择器帮助开发者达到这一目的,让开发者可以更少的处理复杂选择过程与性能优化,更多专 ...

  9. SQL视频总结

    SQL是英文Structured Query Language的缩写,意思为结构化查询语言. SQL语言的主要功能就是同各种数据库建立联系,进行沟通.SQL被作为关系型数据库管理系统的标准语言. SQ ...

  10. 【bzoj4894】天赋 矩阵树定理

    题目描述 小明有许多潜在的天赋,他希望学习这些天赋来变得更强.正如许多游戏中一样,小明也有n种潜在的天赋,但有一些天赋必须是要有前置天赋才能够学习得到的.也就是说,有一些天赋必须是要在学习了另一个天赋 ...