[BZOJ3872][Poi2014]Ant colony

试题描述

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 g groups of m1,m2,...,mg ants 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 d outgoing corridors yet unexplored by the group, the group divides into d 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 d.
The following figure depicts m ants upon entering a chamber with three outgoing unexplored corridors, dividing themselves into three (equal) groups of floor(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.
给定一棵有n个节点的树。在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁。这些蚂蚁会相继进入树中,而且要保证每一时刻每个节点最多只有一群蚂蚁。这些蚂蚁会按以下方式前进:
·在即将离开某个度数为d+1的点时,该群蚂蚁有d个方向还没有走过,这群蚂蚁就会分裂成d群,每群数量都相等。如果d=0,那么蚂蚁会离开这棵树。
·如果蚂蚁不能等分,那么蚂蚁之间会互相吞噬,直到可以等分为止,即一群蚂蚁有m只,要分成d组,每组将会有floor(m/d)只,如下图。
 
一只饥饿的食蚁兽埋伏在一条边上,如果有一群蚂蚁通过这条边,并且数量恰为k只,它就会吞掉这群蚂蚁。请计算一共有多少只蚂蚁会被吞掉。

输入

The first line of the standard input contains three integers n, g, k (2<=n,g<=1000000, 1<=k<=10^9), 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 m[1],m[2],...,m[g](1<=m[i]<=10^9), separated by single spaces, where m[i] gives 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 a[i],b[i] (1<=a[i],b[i]<=n), separated by a single space, that indicate that the chambers no.a[i] and b[i] are linked by a corridor. The anteater has dug into the corridor that appears first on input.
第一行包含三个整数n,g,k,表示点数、蚂蚁群数以及k。
第二行包含g个整数m[1],m[2],...,m[g],表示每群蚂蚁中蚂蚁的数量。
接下来n-1行每行两个整数,表示一条边,食蚁兽埋伏在输入的第一条边上。

输出

Your program should print to the standard output a single line containing a single integer: the number of ants eaten by the anteater.
一个整数,即食蚁兽能吃掉的蚂蚁的数量。

输入示例


输出示例


数据规模及约定

见“输入

题解

因为只有一只食蚁兽,所以可以从它所在的边开始 BFS,求出每条边经过的蚂蚁上限和下限,扩展到叶节点后二分找有几群蚂蚁在下界和上界范围内累计答案即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 1000010
#define maxm 2000010
#define LL long long
int n, g, k, m, head[maxn], next[maxm], to[maxm], ind[maxn];
LL ants[maxn], mxa; void AddEdge(int a, int b) {
to[++m] = b; next[m] = head[a]; head[a] = m;
swap(a, b);
to[++m] = b; next[m] = head[a]; head[a] = m;
return ;
} bool vis[maxn];
int Q[maxn], hd, tl;
LL low[maxn], upp[maxn];
void BFS(int s) {
hd = tl = 0; Q[++tl] = s;
while(hd < tl) {
int u = Q[++hd];
// printf("%d %lld %lld\n", u, low[u], upp[u]);
for(int e = head[u]; e; e = next[e]) if(!vis[to[e]]) {
Q[++tl] = to[e]; vis[to[e]] = 1;
if(low[u] * ((LL)ind[u] - 1) > mxa) low[to[e]] = mxa + 1;
else low[to[e]] = low[u] * ((LL)ind[u] - 1);
if(upp[u] * ((LL)ind[u] - 1) + ind[u] - 2 > mxa) upp[to[e]] = mxa + 1;
else upp[to[e]] = upp[u] * ((LL)ind[u] - 1) + ind[u] - 2;
}
}
return ;
} int main() {
// freopen("data.in", "r", stdin);
// freopen("data.out", "w", stdout);
n = read(); g = read(); k = read();
for(int i = 1; i <= g; i++) ants[i] = read(), mxa = max(mxa, ants[i]); int sta, stb;
for(int i = 1; i < n; i++) {
int a = read(), b = read();
AddEdge(a, b);
ind[a]++; ind[b]++;
if(i == 1) sta = a, stb = b;
}
vis[sta] = vis[stb] = 1;
low[sta] = low[stb] = upp[sta] = upp[stb] = (LL)k;
BFS(sta);
BFS(stb); sort(ants + 1, ants + g + 1);
LL ans = 0;
// for(int i = 1; i <= n; i++) if(ind[i] == 1) printf("%d: %lld %lld\n", i, low[i], upp[i]);
for(int i = 1; i <= n; i++) if(ind[i] == 1) {
int l = lower_bound(ants + 1, ants + g + 1, low[i]) - ants;
int r = upper_bound(ants + 1, ants + g + 1, upp[i]) - ants;
if(r > g || ants[r] > upp[i]) r--;
ans += (LL)(r - l + 1);
// printf("leaves: %d %d %d\n", i, l, r);
}
ans *= (LL)k;
printf("%lld", ans); return 0;
}

[BZOJ3872][Poi2014]Ant colony的更多相关文章

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

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

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

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

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

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

  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

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

  6. [POI2014]Ant colony

    题目大意: 给定一棵$n(n\le10^6)$个结点的树.在每个叶子结点,有$g$群蚂蚁要从外面进来,其中第$i$群有$m_i$只蚂蚁.这些蚂蚁依次爬树(一群蚂蚁爬完后才会爬另一群),若当前经过结点度 ...

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

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

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

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

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

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

随机推荐

  1. [bzoj 1911][Apio 2010]特别行动队(斜率优化DP)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1911 分析: 首先可以的到裸的方程f[i]=max{f[j]+a*(Si-Sj)^2+b*(S ...

  2. angular的工具方法笔记(equals, HashKey)

    分别是angular脏值检测的工具方法equals和 类HashKey的使用方法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi ...

  3. git查看某个文件的修改历史及具体修改内容

    有时候在比对代码时,看到某些改动,但不清楚这个改动的作者和原因,也不知道对应的BUG号,也就是说无从查到这些改动的具体原因了- [注]:某个文件的改动是有限次的,而且每次代码修改的提交都会有commi ...

  4. Daily Scrum – 1/4

    Meeting Minutes 大家讨论了一下作业的内容,以及用户的反馈,商量了一下长期计划(naive)的完成方式. 好像有些时候用户测试的时候会崩溃,不过我们自己用的时候一直没有出现过,分析可能是 ...

  5. java_WEB开发 防刷新

    客户端处理: 面对客户端我们可以使用Javascript脚本来解决,如下 1.重复刷新.重复提交 Ways One:设置一个变量,只允许提交一次. <script language=" ...

  6. SQLHelper初实现---杨中科版(易懂,代码多点)

    public class SQLHelper { //获取连接字符串,,引用Configurationl类库,并引用命名空间using System.Configuration; private st ...

  7. Python 之我见

    读音 Python(KK 英语发音:/ˈpaɪθən/) 序言 其实早前就已经接触了python这个功能强大的脚本语言,但是那时只是基于兴趣而学习,目的性并不是很强,所以学习的并不是很深入.最近由于闲 ...

  8. groovy-脚本和类

    在groovy中定义类和java中是一样的.类的方法可以是static,也可以是非static的. groovy中的方法可以是public, protected, private,同时也支持java中 ...

  9. MyEclipse使用SVN进行项目版本控制

    一.搭建SVN服务器. 例如,使用VisualSVN Server,下载后安装. (1)在Repositories(版本库)上右击,新建Repository,选择Regular FSFS reposi ...

  10. 使用Android Studio搭建Android集成开发环境

    有很长一段时间没有更新博客了,最近实在是太忙了,没有时间去总结,现在终于可以有时间去总结一些Android上面的东西了,很久以前写过这篇关于使用Android Studio搭建Android集成开发环 ...