题目描述

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:

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

  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即为所求。
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #define MAXN 1000001
  6. using namespace std;
  7. int n,g,K;
  8. int s,t,tot;
  9. long long ans;
  10. int outo[MAXN],dad[MAXN];
  11. int to[MAXN],net[MAXN],head[MAXN];
  12. long long m[MAXN],minn[MAXN],maxn[MAXN];
  13. void add(int u,int v){
  14. to[++tot]=v;net[tot]=head[u];head[u]=tot;
  15. to[++tot]=u;net[tot]=head[v];head[v]=tot;
  16. }
  17. void dfs(int now){
  18. for(int i=head[now];i;i=net[i])
  19. if(dad[now]!=to[i]){
  20. dad[to[i]]=now;
  21. outo[now]++;
  22. }
  23. for(int i=head[now];i;i=net[i])
  24. if(dad[now]!=to[i]){
  25. minn[to[i]]=minn[now]*outo[now];
  26. maxn[to[i]]=(maxn[now]+)*outo[now]-;
  27. maxn[to[i]]=min(maxn[to[i]],m[g]);
  28. if(minn[to[i]]<=m[g])
  29. dfs(to[i]);
  30. }
  31. }
  32. long long cal(long long x){
  33. int l=,r=g,bns=;
  34. while(l<=r){
  35. int mid=(l+r)/;
  36. if(m[mid]<x){
  37. bns=max(bns,mid);
  38. l=mid+;
  39. }
  40. else r=mid-;
  41. }
  42. return bns;
  43. }
  44. int main(){
  45. scanf("%d%d%d",&n,&g,&K);
  46. for(int i=;i<=g;i++) scanf("%d",&m[i]);
  47. sort(m+,m++g);
  48. scanf("%d%d",&s,&t);
  49. for(int i=;i<n;i++){
  50. int x,y;
  51. scanf("%d%d",&x,&y);
  52. add(x,y);
  53. }
  54. minn[s]=maxn[s]=minn[t]=maxn[t]=K;
  55. dfs(s);
  56. dfs(t);
  57. for(int i=;i<=n;i++)
  58. if(!outo[i])
  59. ans+=cal(maxn[i]+)-cal(minn[i]);
  60. cout<<ans*K;
  61. }

洛谷 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. Linux内核project导论——网络:Netfilter概览

    简单介绍 最早的内核包过滤机制是ipfwadm.后来是ipchains.再后来就是iptables/netfilter了. 再往后,也就是如今是nftables. 只是nftables与iptable ...

  2. R语言基础-数组和列表

    数组(array) 一维数据是向量,二维数据是矩阵,数组是向量和矩阵的直接推广,是由三维或三维以上的数据构成的. 数组函数是array(),语法是:array(dadta, dim),当中data必须 ...

  3. SSD纠错码向LDPC码演变

    作者:Stephen Bates SSD控制器芯片中採用的纠错编码(ECCs)的类型正在发生一场演变.相信很多这篇博文的读者对此都有所了解.传统上採用的纠错码是基于群变换的博斯-查德胡里-霍昆格母(B ...

  4. Android Handler 具体解释

    Android开发中常常使用Handler来实现"跨越线程(Activity)更新UI".本文将从源代码角度回答:为什么使用Handler可以跨线程更新UI?为什么跨线程更新UI一 ...

  5. multiset多重集合容器

    跟set集合容器相比,multiset多重集合容器也使用红黑树组织元素,仅仅是multiset多重集合容器同意将反复的元素键值插入.元素的搜索依旧具有对数级的算法时间复杂度,find和equal_ra ...

  6. ubuntu16.04+caffe训练mnist数据集

    1.   caffe-master文件夹权限修改 下载的caffe源码编译的caffe-master文件夹貌似没有写入权限,输入以下命令修改: sudo chmod -R 777 ~/caffe-ma ...

  7. Oracle RAC 全局等待事件 gc current block busy 和 gc cr multi block request 说明--转载(http://blog.csdn.net/tianlesoftware/article/details/7777511)

    一.RAC 全局等待事件说明 在RAC环境中,和全局调整缓存相关的最常见的等待事件是global cache cr request,global cache busy和equeue. 当一个进程访问需 ...

  8. [AHOI 2009] 同类分布

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1799 [算法] 数位DP [代码] #include<bits/stdc++. ...

  9. HTML5学习笔记(三):标识文本的语义元素

    1.<time>元素:标注日期和时间 日期格式:YYYY-MM-DD,如2016-04-13: 时间格式(24小时制):HH-MM,如15:31: 最后,组合以上规则就可以制定具体的日期和 ...

  10. Docker运行程序报错 WARNING: IPv4 forwarding is disabled. Networking will not work

    WARNING: IPv4 forwarding is disabled. Networking will not work.   第一步:vi /usr/lib/sysctl.d/00-system ...