题目描述

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_gm​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 dd outgoing corridors yet unexplored by the group,the group divides into dd groups 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≤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 nn.

The second line contains gg integers m_1,m_2,\cdots,m_gm​1​​,m​2​​,⋯,m​g​​ (1\le m_i\le 10^91≤m​i​​≤10​9​​), separated by single spaces, where m_im​i​​ 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_ia​i​​,b_ib​i​​ (1\le a_i,b_i\le n1≤a​i​​,b​i​​≤n), separated by a single space, that indicate that the chambers no. a_ia​i​​ and b_ib​i​​ 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只蚂蚁就被吃掉,问一共有多少只蚂蚁被吃掉

题意:

题目描述:

给定一棵有n个节点的树。在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁。这些蚂蚁会相继进入树中,而且要保证每一时刻每个节点最多只有一群蚂蚁。这些蚂蚁会按以下方式前进:
·在即将离开某个度数为d+1的点时,该群蚂蚁有d个方向还没有走过,这群蚂蚁就会分裂成d群,每群数量都相等。如果d=0,那么蚂蚁会离开这棵树。
·如果蚂蚁不能等分,那么蚂蚁之间会互相吞噬,直到可以等分为止,即一群蚂蚁有m只,要分成d组,每组将会有floor(m/d)只,如下图。
一只饥饿的食蚁兽埋伏在一条边上,如果有一群蚂蚁通过这条边,并且数量恰为k只,它就会吞掉这群蚂蚁。请计算一共有多少只蚂蚁会被吞掉。
输入描述:
第一行包含三个整数n,g,k,表示点数、蚂蚁群数以及k。
第二行包含g个整数m[1],m[2],...,m[g],表示每群蚂蚁中蚂蚁的数量。
接下来n-1行每行两个整数,表示一条边,食蚁兽埋伏在输入的第一条边上。
思路:
这个题从叶子节点去算的话显然很困难,所以我们可以从食蚁兽所在的两个点开始倒着搞。
 minn和maxn记录每个节点最多和最少有多少个蚂蚁才能让食蚁兽恰好吃到k只。然后,分别以这两个点为根节点,dfs一下求出所有的minn和maxn。 
接着,二分答案,求一下对于每个节点,g群蚂蚁中符合条件的蚂蚁的群数。
最后,群数*k即为所求。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 1000001
using namespace std;
int n,g,K;
int s,t,tot;
long long ans;
int outo[MAXN],dad[MAXN];
int to[MAXN],net[MAXN],head[MAXN];
long long m[MAXN],minn[MAXN],maxn[MAXN];
void add(int u,int v){
to[++tot]=v;net[tot]=head[u];head[u]=tot;
to[++tot]=u;net[tot]=head[v];head[v]=tot;
}
void dfs(int now){
for(int i=head[now];i;i=net[i])
if(dad[now]!=to[i]){
dad[to[i]]=now;
outo[now]++;
}
for(int i=head[now];i;i=net[i])
if(dad[now]!=to[i]){
minn[to[i]]=minn[now]*outo[now];
maxn[to[i]]=(maxn[now]+)*outo[now]-;
maxn[to[i]]=min(maxn[to[i]],m[g]);
if(minn[to[i]]<=m[g])
dfs(to[i]);
}
}
long long cal(long long x){
int l=,r=g,bns=;
while(l<=r){
int mid=(l+r)/;
if(m[mid]<x){
bns=max(bns,mid);
l=mid+;
}
else r=mid-;
}
return bns;
}
int main(){
scanf("%d%d%d",&n,&g,&K);
for(int i=;i<=g;i++) scanf("%d",&m[i]);
sort(m+,m++g);
scanf("%d%d",&s,&t);
for(int i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
}
minn[s]=maxn[s]=minn[t]=maxn[t]=K;
dfs(s);
dfs(t);
for(int i=;i<=n;i++)
if(!outo[i])
ans+=cal(maxn[i]+)-cal(minn[i]);
cout<<ans*K;
}

洛谷 P3576 [POI2014]MRO-Ant colony的更多相关文章

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

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

  2. 洛谷P3576 [POI2014]MRO-Ant colony [二分答案,树形DP]

    题目传送门 MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. The ant h ...

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

    https://www.luogu.org/problem/show?pid=3576 题目描述 The ants are scavenging an abandoned ant hill in se ...

  4. 洛谷 P3580 - [POI2014]ZAL-Freight(单调队列优化 dp)

    洛谷题面传送门 考虑一个平凡的 DP:我们设 \(dp_i\) 表示前 \(i\) 辆车一来一回所需的最小时间. 注意到我们每次肯定会让某一段连续的火车一趟过去又一趟回来,故转移可以枚举上一段结束位置 ...

  5. 洛谷 P3573 [POI2014]RAJ-Rally 解题报告

    P3573 [POI2014]RAJ-Rally 题意: 给定一个\(N\)个点\(M\)条边的有向无环图,每条边长度都是\(1\). 请找到一个点,使得删掉这个点后剩余的图中的最长路径最短. 输入输 ...

  6. 洛谷P3572 [POI2014]PTA-Little Bird

    P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...

  7. 2018.09.14 洛谷P3567 [POI2014]KUR-Couriers(主席树)

    传送门 简单主席树啊. 但听说有随机算法可以秒掉%%%(本蒟蒻并不会) 直接维护值域内所有数的出现次数之和. 当这个值不大于区间总长度的一半时显然不存在合法的数. 这样在主席树上二分查值就行了. 代码 ...

  8. 洛谷P3567[POI2014]KUR-Couriers(主席树+二分)

    题意:给一个数列,每次询问一个区间内有没有一个数出现次数超过一半 题解: 最近比赛太多,都没时间切水题了,刚好日推了道主席树裸题,就写了一下 然后 WA80 WA80 WA0 WA90 WA80 ?? ...

  9. 【刷题】洛谷 P3573 [POI2014]RAJ-Rally

    题目描述 An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long di ...

随机推荐

  1. etymology-R

    1)vor = to eat devour vt. 狼吞虎咽地吃光: 吞没,毁灭: 目不转睛地看[de-向下+vour-吃] voracity  n.贪食,贪婪.拉丁词根vor-,vorac-表示吞食 ...

  2. C专家编程之为什么C语言把数组形參当做指针:数组/指针实參

    #include<stdio.h> void print_array_test(char ca[]) { printf("ca : %s\n",ca); printf( ...

  3. unity3d 中动画的帧事件

    代码事件监听 using UnityEngine; using System.Collections; public class BoxEventScript : MonoBehaviour { vo ...

  4. 16进制颜色字符串转为UIColor

    //16进制颜色(html颜色值)字符串转为UIColor +(UIColor *) hexStringToColor: (NSString *) stringToConvert {      NSS ...

  5. 0x04 二分

    二分.三分其实没什么.. 但是真心觉得市面上的朴素二分打法千奇百怪,假如是像我的标程应该是比较稳妥的,然而poj2018那题(前缀和又想起来了)是向下取整,精度有点问题(经常拍出一些什么xxx.999 ...

  6. 英语发音规则---B字母

    英语发音规则---B字母 一.总结 一句话总结: 1.B发[b]音? bike [baɪk] n. 自行车 bus [bʌs] n. 公共汽车 bag [bæg] n. 袋:猎获物 baby ['be ...

  7. ffmpeg键盘命令响应程序详解

    一.对终端进行读写 当一个程序在命令提示符中被调用时, shell负责将标准输入和标准输出流连接到你的程序, 实现程序与用户间的交互.   1. 标准模式和非标准模式 在默认情况下, 只有用户按下回车 ...

  8. ES6常用特性

    以下是ES6排名前十的最佳特性列表(排名不分先后): Default Parameters(默认参数) in ES6 Template Literals (模板文本)in ES6 Multi-line ...

  9. RedHat/CentOS(Linux)双网卡bond(mode=6)

    1. 将/etc/sysconfig/network-scripts/ifcfg-ens1f0和ifcfg-ens1f1文件备份到root目录下2. 修改/etc/sysconfig/network- ...

  10. 备份和恢复ZBrush文件

    ZBrush可以自动保存绘图的备份副本,并在发生系统错误时提醒您恢复备份副本.当ZBrush软件遇到崩溃.导致错误.非正常退出的时候,可能之前所做的努力就会功亏一篑,那么,在ZBrush软件中能否将文 ...