Description

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 \(m_{1},m_{2},...,m_{g}\) 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 (\(\lfloor \frac{m}{3} \rfloor\)) 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\)组,每组将会有\(\lfloor \frac{m}{d} \rfloor\)只,如下图。

一只饥饿的食蚁兽埋伏在一条边上,如果有一群蚂蚁通过这条边,并且数量恰为\(k\)只,它就会吞掉这群蚂蚁。请计算一共有多少只蚂蚁会被吞掉。

Input

The first line of the standard input contains three integers \(n, g, k\) (\(2 \le n,g \le 1000000,1 \le k \le 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 \le m[i] \le 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 \le a[i],b[i] \le 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\)行每行两个整数,表示一条边,食蚁兽埋伏在输入的第一条边上。

Output

Your program should print to the standard output a single line containing a single integer: the number of ants eaten by the anteater.

一个整数,即食蚁兽能吃掉的蚂蚁的数量。

Sample Input

7 5 3

3 4 1 9 11

1 2

1 4

4 3

4 5

4 6

6 7

Sample Output

21

一道比较水的题目吧(我都想得出,还不水吗???)

从食蚁兽潜伏的那两条线段开始bfs,初步推出食蚁兽能吃掉\(k\)只蚂蚁,需要有几只蚂蚁到达该点(该范围一定是一段连续的区间)。递推到叶子之后统计答案即可(lower_bound区间左端点\(l\),upper_bound区间右端点\(r\),\(r-l\)即为对于答案的贡献)。

#include<queue>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std; typedef long long ll;
#define maxn (1000010)
int side[maxn],toit[maxn*2],next[maxn*2],m[maxn],g,k,n;
int S,T,cnt,d[maxn];
bool in[maxn]; ll arr[maxn][2],ans,inf; inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} inline void add(int a,int b)
{
next[++cnt] = side[a]; side[a] = cnt;
toit[cnt] = b; ++d[a];
} inline void ins(int a,int b) { add(a,b); add(b,a); } inline void bfs()
{
queue <int> team;
arr[S][0] = (ll)k*(ll)max((d[S]-1),1); arr[S][1] = min((ll)(k+1)*(ll)max((d[S]-1),1)-1LL,inf);
arr[T][0] = (ll)k*(ll)max((d[T]-1),1); arr[T][1] = min((ll)(k+1)*(ll)max((d[T]-1),1)-1LL,inf);
in[S] = in[T] = true;
if (arr[S][0] < inf) team.push(S); if (arr[T][0] < inf) team.push(T);
while (!team.empty())
{
int now = team.front(); team.pop();
if (d[now] == 1)
{
int l = lower_bound(m+1,m+g+1,arr[now][0])-m,r = upper_bound(m+1,m+g+1,arr[now][1])-m;
ans += (ll)(r-l)*(ll)k;
}
for (int i = side[now];i;i = next[i])
{
int v = toit[i];
if (in[toit[i]]) continue;
in[toit[i]] = true;
arr[v][0] = arr[now][0]*(ll)(max(d[v]-1,1));
arr[v][1] = min((arr[now][1]+1LL)*(ll)(max(d[v]-1,1))-1LL,inf);
if (arr[v][0] < inf) team.push(v);
}
}
} int main()
{
freopen("3872.in","r",stdin);
freopen("3872.out","w",stdout);
n = read(); g = read(); k = read();
for (int i = 1;i <= g;++i) m[i] = read();
sort(m+1,m+g+1); inf = m[g]+1;
S = read(); T = read();
ins(S,T);
for (int i = 2;i < n;++i) ins(read(),read());
bfs(); printf("%lld",ans);
fclose(stdin); fclose(stdout);
return 0;
}

BZOJ 3872 Ant colony的更多相关文章

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

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

  2. [BZOJ3872][Poi2014]Ant colony

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

  3. Codeforces 474 F. Ant colony

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

  4. 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 ...

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

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

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

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

  7. CodeForces 474F Ant colony ST+二分

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

  8. Codeforces G. Ant colony

    题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...

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

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

随机推荐

  1. 系统调用和中断处理的异同(以Linux MIPS为例)

    在Linux下写一个驱动时候遇到的读操作性能问题,让我想一窥系统调用的处理流程,以查出问题的root cause.很多书把它和中断处理放在一起讲,但是又没有哪本书说清楚了,看来只有代码才能说明一切.以 ...

  2. 学习NodeJS第一天:node.js引言

    Node.JS 是资深 C 程序猿 Ryan Dahl(http://four.livejournal.com/)的作品,根据 Google 著名的开源 JavaScript 引擎 V8 来进行二次开 ...

  3. 3高并发server:多路IO之epoll

     1 epoll epoll是Linux下多路复用IO接口select/poll的增强版本号,它能显著提高程序在大量并.发连接中仅仅有少量活跃的情况下的系统CPU利用率,由于它会复用文件描写叙述符 ...

  4. 详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)

    前言: 本篇文章不是分享collectionView的详细使用教程, 而是属于比较’高级’的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, ...

  5. Java基础知识强化之集合框架笔记19:List集合迭代器使用之 并发修改异常的产生原因 以及 解决方案

    1. 我有一个集合,如下,请问,我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现. ConcurrentModi ...

  6. oracle:变长数组varray,嵌套表,集合

    创建变长数组类型 ) );  这个变长数组最多可以容纳两个数据,数据的类型为 varchar2(50) 更改元素类型的大小或精度 可以更改变长数组类型和嵌套表类型 元素的大小. ALTER TYPE ...

  7. Axiom3D学习日记 2.介绍SceneManager,SceneNode,Entity

    SceneManager(场景管理类) 所有出现在屏幕里的东西都受SceneManager管理(最好是这样),当你放置对象在场景里,SceneManager就会跟踪他们的位置,当你为场景创建一个相机, ...

  8. ORACLE用户管理方式下备份数据和复制数据库

    首先要明确的是,oracle数据库的备份可以分为逻辑备份和物理备份.           逻辑备份的是通过数据导出对数据进行备份,主要方式有老式的IMP/EXP和数据泵灯方式.适合变化较少的数据库,而 ...

  9. 使用EasyUI导入的js顺序

    使用Jquery Easy UI要导入的js顺序<1>.引用Jquery的Js文件<script src="jquery-easyui-1.3.4/jquery-1.8.0 ...

  10. js submit的問題

    form 里面有input name="submit"的时候 $('#seachform').submit();不起作用