[POI2014]Ant colony
题目大意:
给定一棵$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的更多相关文章
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- 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+二分
[BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...
- bzoj 3872 [Poi2014]Ant colony——二分答案
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 可以倒推出每个叶子节点可以接受的值域.然后每个叶子二分有多少个区间符合即可. 注意一开 ...
- bzoj 3872: [Poi2014]Ant colony【树形dp+二分】
啊我把分子分母混了WA了好几次-- 就是从食蚁兽在的边段成两棵树,然后dp下去可取的蚂蚁数量区间,也就是每次转移是l[e[i].to]=l[u](d[u]-1),r[e[i].to]=(r[u]+1) ...
- $bzoj3872\ [Poi2014]\ Ant\ colony$ 二分+$dp$
正解:二分+$dp$ 解题报告: 传送门$QwQ$ 一年过去了依然没有头绪,,,$gql$的$NOIp$必将惨败了$kk$. 考虑倒推,因为知道知道除数和答案,所以可以推出被除数的范围,然后一路推到叶 ...
- Luogu3576 POI2014 MRO-Ant colony 【树形DP】*
Luogu3576 POI2014 MRO-Ant colony The ants are scavenging an abandoned ant hill in search of food. Th ...
- 洛谷——P3576 [POI2014]MRO-Ant colony
P3576 [POI2014]MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. ...
- [bzoj3872][Poi2014]Ant colony_树形dp
Ant colony bzoj-3872 Poi-2014 题目大意:说不明白.....题目链接 注释:略. 想法:两个思路都行. 反正我们就是要求出每个叶子节点到根节点的每个路径权值积. 可以将边做 ...
随机推荐
- day04_08-while查询所有行
<?php $link = @mysql_connect('localhost','root',''); mysql_query('use test',$link); mysql_query(' ...
- python学习总结 --函数基础
函数基础 ### 函数简介 - 定义:具有特定功能的一段代码 - 优点: - 可以减少代码的重复书写 - 可以将功能的实现着和使用者分开,可以提高开发效率 - 分类: - 库函数:print.inpu ...
- ubantu 系统常见问题
1 搜狗输入法错误: 删除home路径下的 .config/SouGouP...整个文件夹并重启 2 ubantu系统更新:sudo apt-get update 获取最近更新的软件包列表 ...
- mac book pro 安装keras (无gpu)
转自http://www.jianshu.com/p/01dc42595733 注:Macbook pro 13' 没有NVIDIA的显卡,没办法CUDA编程,所以下面都是CPU编程. 1. 安装ho ...
- PAT 甲级 1011 World Cup Betting
https://pintia.cn/problem-sets/994805342720868352/problems/994805504927186944 With the 2010 FIFA Wor ...
- mogodb gui
mogodb gui https://robomongo.org/ https://github.com/Studio3T/robomongo https://www.imooc.com/learn/ ...
- 湘潭邀请赛 2018 I Longest Increasing Subsequence
题意: 给出一个长度为n的序列,序列中包含0.定义f(i)为把所有0变成i之后的Lis长度,求∑ni=1i⋅f(i). 题解: 设不考虑0的Lis长度为L,那么对于每个f(i),值为L或L+1. 预处 ...
- [poj] 1204 Word Puzzles || AC自动机
原题 给个X*Y的字符串矩阵,W个询问,对每个询问输出这个串出现的位置及方向,共有8个方向,顺时针开始分别用A~H表示 AC自动机的板子题. 对于待匹配串建一个自动机,然后从矩阵的四周分别沿八个方向跑 ...
- JAVA项目-嗖嗖移动
/** * 移动卡类 */ public class MobileCard { private String cardNumber; //卡号 private String userName; //用 ...
- 类复制 MemberwiseClone与Clone(深 浅 Clone)
MemberwiseClone 方法创建一个浅表副本,具体来说就是创建一个新对象,然后将当前对象的非静态字段复制到该新对象.如果字段是值类型的,则对该字段执行逐位复制.如果字段是引用类型,则复制引用但 ...