题目大意:
  给定一棵$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. ASP.net MVC入门及Razor语法

    一.MVC入门: 1.MVC简介 约定大于配置 2.MVC访问流程 csthml模板(razor模板)就是简化HTML的拼接的模板,最终还是生成html给浏览器显示,不能直接访问cshtml文件. 二 ...

  3. java中unmodifiableList方法的应用场景

    java对象中primitive类型变量可以通过不提供set方法保证不被修改,但对象的List成员在提供get方法后,就可以随意add.remove改变其结构,这不是希望的结果.网上看了下,发现Col ...

  4. 2017 多校4 Security Check

    2017 多校4 Security Check 题意: 有\(A_i\)和\(B_i\)两个长度为\(n\)的队列过安检,当\(|A_i-B_j|>K\)的时候, \(A_i和B_j\)是可以同 ...

  5. Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例

    如何用一条sql语句实现批量更新?mysql并没有提供直接的方法来实现批量更新,但是可以用点小技巧来实现. 复制代码 代码如下: UPDATE mytable SET myfield = CASE i ...

  6. Installing patches on an ESXi 5.x by the command

    Purpose This article outlines the procedure for installing patches on an ESXi 5.x host from the comm ...

  7. Matlab 几种卷积的实现与比较(conv与filter,conv2与filter2)

    Matlab 几种卷积的实现与比较(conv与filter,conv2与filter2)     最近在做控制算法实现的时候,对于其中参杂的各种差分.卷积很头疼,就在网上搜集了些资料,汇总于此,以做备 ...

  8. Android JSON

    转自:http://www.open-open.com/lib/view/open1326376799874.html JSON的定义: 一 种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性 ...

  9. C语言浮点数存储方式

    对于浮点类型的数据采用单精度类型(float)和双精度类型(double)来存储,float数据占用 32bit,double数据占用 64bit.其实不论是float类型还是double类型,在计算 ...

  10. Linux忘记Mysql原来root的密码

    Linux忘记Mysql原来root的密码 1.首先需要有linux操作系统root的权限 2.ps aux | grep mysql (或者 systemctl status mysql) 发现my ...