题目传送门

MRO-Ant colony

题目描述

The ants are scavenging an abandoned ant hill in search of food.

The ant hill has nn chambers and n-1n−1 corridors connecting them.

We know that each chamber can be reached via a unique path from every other chamber.

In other words, the chambers and the corridors form a tree.

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 gg groups of m_1,m_2,\cdots,m_gm1​,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 dd outgoing corridors yet unexplored by the group,the group divides into ddgroups of equal size. Each newly created group follows one of the d corridors.If d=0d=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 dd.

The following figure depicts mm ants upon entering a chamber with three outgoing unexplored corridors, dividing themselves into three (equal) groups of \left \lfloor m/3 \right \rfloor⌊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 kk ants.

We want to know how many ants the anteater will eat.

给一棵树,对于每个叶子节点,都有g群蚂蚁要从外面进来,每群蚂蚁在行进过程中只要碰到岔路,就将平均地分成岔路口数-1那么多份,然后平均地走向剩下的那些岔路口,余下的蚂蚁自动消失,树上有一个关键边,假如有一群蚂蚁通过了这条边且数量恰好为k,这k只蚂蚁就被吃掉,问一共有多少只蚂蚁被吃掉

输入输出格式

输入格式:

The first line of the standard input contains three integers nn, gg, kk(2\le n,g\le 1\ 000\ 0002≤n,g≤1 000 000, 1\le k\le 10^91≤k≤109), 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 nn.

The second line contains gg integers m_1,m_2,\cdots,m_gm1​,m2​,⋯,mg​ (1\le m_i\le 10^91≤mi​≤109), separated by single spaces, where m_imi​ gives the number of ants in the ii-th group at every entrance to the ant hill. The n-1n−1 lines that follow describe the corridors within the ant hill;the ii-th such line contains two integers a_iai​,b_ibi​ (1\le a_i,b_i\le n1≤ai​,bi​≤n), separated by a single space, that indicate that the chambers no. a_iai​ and b_ibi​ are linked by a corridor. The anteater has dug into the corridor that appears first on input.

输出格式:

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

输入输出样例

输入样例#1:

7 5 3
3 4 1 9 11
1 2
1 4
4 3
4 5
4 6
6 7
输出样例#1:

21

说明

给一棵树,对于每个叶子节点,都有g群蚂蚁要从外面进来,每群蚂蚁在行进过程中只要碰到岔路,就将平均地分成岔路口数-1那么多份,然后平均地走向剩下的那些岔路口,余下的蚂蚁自动消失,树上有一个关键边,假如有一群蚂蚁通过了这条边且数量恰好为k,这k只蚂蚁就被吃掉,问一共有多少只蚂蚁被吃掉


  分析:

  一道比较考思维的题。

  如果按照题目的要求从叶子节点开始做的话,很难有比较优秀的方法。

  那么就换一种方式,从两个给定的根开始。因为给定的一条边一定会把一棵树分割成两部分,所以我们可以直接把这棵树当作两棵树来处理。对于每一棵树,从根节点开始$DFS$,确定从这个点到达根节点时如果要正好有$k$只蚂蚁,在这个点至少需要多少蚂蚁,至多能有多少蚂蚁。再对给定的每一群蚂蚁排序。处理完以后,对于所有的叶子节点二分答案找到每个叶子节点有多少群蚂蚁合法,最后输出答案就行了。

  讲的比较抽象,可以看代码理解。

  Code:

//It is made by HolseLee on 5th Nov 2018
//Luogu.org P3576
#include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int N=1e6+;
int n,g,k,head[N],cnte,dg[N],fa[N],sx,sy;
ll ans,maxn[N],minn[N],c[N];
struct Edge { int to,nxt; }e[N<<]; inline int read()
{
char ch=getchar(); int x=; bool flag=false;
while( ch<'' || ch>'' ) {
if( ch=='-' ) flag=true; ch=getchar();
}
while( ch>='' && ch<='' ) {
x=x*+ch-''; ch=getchar();
}
return flag ? -x : x;
} inline void add(int x,int y)
{
e[++cnte].to=y, e[cnte].nxt=head[x], head[x]=cnte;
e[++cnte].to=x, e[cnte].nxt=head[y], head[y]=cnte;
} void dfs(int x)
{
for(int i=head[x]; i; i=e[i].nxt) {
if( e[i].to!=fa[x] ) {
fa[e[i].to]=x; dg[x]++;
}
}
for(int i=head[x],y; i; i=e[i].nxt) {
y=e[i].to;
if( y==fa[x] ) continue;
minn[y]=minn[x]*dg[x];
maxn[y]=(maxn[x]+)*dg[x]-;
maxn[y]=min(maxn[y],c[g]);
if( minn[y]<=c[g] ) dfs(y);
}
} inline ll getans(ll x)
{
int l=,r=g,mid,ret=;
while( l<=r ) {
mid=(l+r)>>;
if( c[mid]<x ) l=mid+,ret=mid;
else r=mid-;
}
return ret;
} int main()
{
n=read(), g=read(), k=read();
for(int i=; i<=g; ++i) c[i]=read();
sort(c+,c+g+);
int x,y; sx=read(), sy=read();
for(int i=; i<n; ++i) {
x=read(), y=read(); add(x,y);
}
maxn[sx]=maxn[sy]=minn[sx]=minn[sy]=k;
dfs(sx);dfs(sy);
for(int i=; i<=n; ++i)
if( !dg[i] ) ans+=getans(maxn[i]+)-getans(minn[i]);
printf("%lld\n",ans*k);
return ;
}

洛谷P3576 [POI2014]MRO-Ant colony [二分答案,树形DP]的更多相关文章

  1. HDU 3586 二分答案+树形DP判定

    HDU 3586 『Link』HDU 3586 『Type』二分答案+树形DP判定 ✡Problem: 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

  2. BZOJ_2097_[Usaco2010 Dec]Exercise 奶牛健美操_二分答案+树形DP

    BZOJ_2097_[Usaco2010 Dec]Exercise 奶牛健美操_二分答案+树形DP Description Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的 ...

  3. 洛谷 P3576 [POI2014]MRO-Ant colony

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

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

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

  5. [NOIP2015提高&洛谷P2678]跳石头 题解(二分答案)

    [NOIP2015提高&洛谷P2678]跳石头 Description 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之 ...

  6. 洛谷P5021 赛道修建 NOIp2018 贪心+二分答案

    正解:贪心+LCA+二分答案 解题报告: 想先港下部分分qwq因为我部分分只拿到了10ptsQAQ(时间不够不是理由,其实还是太弱,所以要想很久,所以才时间不够QAQ m=1 找直径长度,完 一条链 ...

  7. 洛谷P2323 [HNOI2006] 公路修建问题 [二分答案,生成树]

    题目传送门 公路修建问题 题目描述 OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Associa ...

  8. 【洛谷 P1419】 寻找段落(二分答案,单调队列)

    题目链接 开始还以为是尺取.发现行不通. 一看标签二分答案,恍然大悟. 二分一个\(mid\)(实数),把数列里每个数减去\(mid\),然后求前缀和,在用单调队列维护\(sum[i-t\text{~ ...

  9. 洛谷 P2678 [ NOIP 2015 ] 跳石头 —— 二分答案

    题目:https://www.luogu.org/problemnew/show/P2678 二分答案. 代码如下: #include<iostream> #include<cstd ...

随机推荐

  1. 【bzoj2878】 Noi2012—迷失游乐园

    http://www.lydsy.com/JudgeOnline/problem.php?id=2878 (题目链接) 题意 求基环树上以任意点为起点的简单路径期望长度. Solution 啊啊啊好丑 ...

  2. 七、spring boot 1.5.4 集成shiro+cas,实现单点登录和权限控制

    1.安装cas-server-3.5.2 官网:https://github.com/apereo/cas/releases/tag/v3.5.2 下载地址:cas-server-3.5.2-rele ...

  3. Zabbix应用六:Zabbix监控Redis

    利用Zabbix监控Redis Zabbix监控redis就比较简单了,因为zabbix官方提供了监控redis的模版和脚本,而且脚本有nodejs和python两种,下载地址:https://git ...

  4. Spark记录-Scala语句(运算符-if-for-while-try-模式匹配)

    Scala条件运算符 Scala条件运算符在下表中列出. 运算符 操作 描述 && 与 运算符左侧和右侧的值为true.仅当左侧为真时,右侧才被计算. || 或 左侧或右侧的至少一个值 ...

  5. SHELL (2) —— Shell变量的核心基础知识和实践

    摘自:Oldboy Linux运维——SHELL编程实战 Shell变量:用一个固定的字符串(也可能是字符.数字等的组合)代替更多.更复杂的内容,该内容里可能还会包含变量.路径.字符串等其它的内容. ...

  6. Jsp使用遍历List集合

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  7. bzoj千题计划246:bzoj2242: [SDOI2011]计算器

    http://www.lydsy.com/JudgeOnline/problem.php?id=2242 #include<map> #include<cmath> #incl ...

  8. bzoj千题计划211:bzoj1996: [Hnoi2010]chorus 合唱队

    http://www.lydsy.com/JudgeOnline/problem.php?id=1996 f[i][j][0/1] 表示已经排出队形中的[i,j],最后一个插入的人在[i,j]的i或j ...

  9. 20155206 2016-2017-2 《Java程序设计》第8周学习总结

    20155206 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十五章 通用API 15.1 日志 日志API简介 java.util.logging包提 ...

  10. Spark笔记之累加器(Accumulator)

    一.累加器简介 在Spark中如果想在Task计算的时候统计某些事件的数量,使用filter/reduce也可以,但是使用累加器是一种更方便的方式,累加器一个比较经典的应用场景是用来在Spark St ...