洛谷——P3576 [POI2014]MRO-Ant colony
P3576 [POI2014]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 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≤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.

输入输出样例
7 5 3 3 4 1 9 11 1 2 1 4 4 3 4 5 4 6 6 7
21
说明
给一棵树,对于每个叶子节点,都有g群蚂蚁要从外面进来,每群蚂蚁在行进过程中只要碰到岔路,就将平均地分成岔路口数-1那么多份,然后平均地走向剩下的那些岔路口,余下的蚂蚁自动消失,树上有一个关键边,假如有一群蚂蚁通过了这条边且数量恰好为k,这k只蚂蚁就被吃掉,问一共有多少只蚂蚁被吃掉
树形dp
O(≧口≦)O做了一晚上、、、
我们会发现一个很严肃的问题,如果我们从子节点开始做的话会非常难处理(反正蒟蒻表示我不会、、、)
既然正着不会,我们就倒着搞吧、、其次他说的是通过一条边的数量为k,这k只蚂蚁都会被吃掉。然而我们如果直接用边来进行运算的话,既然这样我们直接将边改为这条边的两个节点,然后以这两个节点为根节点,将原来的树处理成两棵树。然后就可以在这两棵树上乱搞了
如图所示:在1~2这条边上如果有k只蚂蚁经过的时候这k只蚂蚁会被吃掉
我们将上面的两棵树抽离成两颗一颗以1为根节点,一棵以2为根节点。
对于每一颗树我们从根节点开始往下进行搜索,更新每一个节点的父亲节点,如果这个点的父亲节点与他相连出去的节点不相同,那么将与他相连节点的父亲节点为当前节点。
我们看上面的图,如果要是蚂蚁经过1~2这条边上的时候蚂蚁的数量达到k时1号节点·与2好节点处的蚂蚁数量也就要为k。
然后我们在从这个各节点倒着计算他下面的节点蚂蚁的数量
我们知道进入蚂蚁的节点一定是子节点,然后每一个子节点往其他的分支那平分蚂蚁,他评分出来的蚂蚁一定是有一波要进入他的父亲节点。这样的话我们可以直接根据他的父亲节点的蚂蚁数目推出当前节点的蚂蚁数目,我们这个地方是不可能求出准确值的,因为他在平分的时候是下取整的。所以我们只能求出一个子节点的蚂蚁数目的范围。
子节点的蚂蚁的数目等于它的父节点的;连出去的边的条数*父节点的蚂蚁个数。
看这个图,你可以发现一个规律它的父节点连出去的边的条数正好等于它的父节点的入读。我们可以在进行dfs的时候处理处每一个点的父亲节点以及该点的入读、
蚂蚁平分,子节点最少的蚂蚁的个数为他父亲节点的蚂蚁的数量*父亲节点的入读,也就是说没有蚂蚁被消灭。最多的蚂蚁的数量为还差一个蚂蚁就可以让每一个分支在多分一只蚂蚁了,极为maxn[t]=(sum[now]+1)*out[now]-1
然后dfs结束的条件的为他已经搜索到最后一层子节点了。当minn[to[i]]<=m[g]是说明还没有到达根节点。
然后我们可以处理处到达叶子节点的时候的蚂蚁的最多数量跟最少数量,然后在判断有多少组蚂蚁满足要求。
最后的可以吃到的蚂蚁的数量为k*满足条件的蚂蚁的组数
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 1100000
#define ll long long
using namespace std;
int n,g,k,x,y,sx,sy,tot;
int fa[N],in[N],head[N];
ll s[N],maxn[N],minn[N],ans;
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
struct Edge
{
    int to,from,next;
}edge[N<<];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int dfs(int now)
{
    for(int i=head[now];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if(fa[now]!=t)
        {
            fa[t]=now;
            in[now]++;
         }
    }
    for(int i=head[now];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if(fa[now]!=t)
        {
            minn[t]=minn[now]*in[now];
            maxn[t]=(maxn[now]+)*;
            maxn[t]=min(maxn[t],s[g]);
            if(minn[t]<=s[g]) dfs(t);
        }
    }
}
ll erfen(ll x)
{
    ,r=g,ans1=;
    while(l<=r)
    {
        ;
        if(s[mid]<x)
        {
            ans1=mid;
            l=mid+;
        }
        ;
    }
    return ans1;
}
int main()
{
    n=read(),g=read(),k=read();
    ;i<=g;i++) scanf("%lld",&s[i]);
    sort(s+,s++g);
    sx=read(),sy=read();
    ;i<n;i++)
    {
        x=read(),y=read();
        add(x,y),add(y,x);
    }
    minn[sx]=minn[sy]=maxn[sx]=maxn[sy]=k;
    dfs(sx),dfs(sy);
    ;i<=n;i++)
     if(!in[i])
      ans+=erfen(maxn[i]+)-erfen(minn[i]);
    printf("%lld",ans*k);
    ;
}
洛谷——P3576 [POI2014]MRO-Ant colony的更多相关文章
- 洛谷 P3576 [POI2014]MRO-Ant colony
		
P3576 [POI2014]MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. ...
 - 洛谷P3576 [POI2014]MRO-Ant colony [二分答案,树形DP]
		
题目传送门 MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. The ant h ...
 - 洛谷—— P3576 [POI2014]MRO-Ant colony
		
https://www.luogu.org/problem/show?pid=3576 题目描述 The ants are scavenging an abandoned ant hill in se ...
 - 洛谷 P3580 - [POI2014]ZAL-Freight(单调队列优化 dp)
		
洛谷题面传送门 考虑一个平凡的 DP:我们设 \(dp_i\) 表示前 \(i\) 辆车一来一回所需的最小时间. 注意到我们每次肯定会让某一段连续的火车一趟过去又一趟回来,故转移可以枚举上一段结束位置 ...
 - 洛谷 P3573 [POI2014]RAJ-Rally 解题报告
		
P3573 [POI2014]RAJ-Rally 题意: 给定一个\(N\)个点\(M\)条边的有向无环图,每条边长度都是\(1\). 请找到一个点,使得删掉这个点后剩余的图中的最长路径最短. 输入输 ...
 - 洛谷P3572 [POI2014]PTA-Little Bird
		
P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...
 - 2018.09.14 洛谷P3567 [POI2014]KUR-Couriers(主席树)
		
传送门 简单主席树啊. 但听说有随机算法可以秒掉%%%(本蒟蒻并不会) 直接维护值域内所有数的出现次数之和. 当这个值不大于区间总长度的一半时显然不存在合法的数. 这样在主席树上二分查值就行了. 代码 ...
 - 洛谷P3567[POI2014]KUR-Couriers(主席树+二分)
		
题意:给一个数列,每次询问一个区间内有没有一个数出现次数超过一半 题解: 最近比赛太多,都没时间切水题了,刚好日推了道主席树裸题,就写了一下 然后 WA80 WA80 WA0 WA90 WA80 ?? ...
 - 【刷题】洛谷 P3573 [POI2014]RAJ-Rally
		
题目描述 An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long di ...
 
随机推荐
- hibernate3缓存(hibernate)
			
一级缓存:当应用程序调用Session 的save() .update() .savaeOrUpdate() .get() 或load() ,以及调用查询接口的list() .iterate() 或f ...
 - Spring框架中的aop操作之一 及aspectjweaver.jar与aopalliance-1.0.jar下载地址 包含beans 注解context 和aop的约束
			
(aspect oriented programming面向切面编程) 首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression ...
 - ios面试题(三)
			
4.写一个setter方法用于完成@property (nonatomic,retain)NSString *name,写一个setter方法用于完成@property(nonatomic,copy) ...
 - ios 序列化
			
1到底这个序列化有啥作用? 面向对象的程序在运行的时候会创建一个复杂的对象图,经常要以二进制的方法序列化这个对象图,这个过程叫做Archiving. 二进制流可以通过网络或写入文件中(来源于某教材的一 ...
 - ios  lazying  load
			
初步写一些自己对于lazyload的看法吧.这篇文章主要针对普通view,对于image相关的的懒加载,准备过几天研究一下在写. 懒加载,又称为延迟加载.通常用法,你有一个UITextField类型的 ...
 - 【技巧:字符串同构】Avendesora
			
判断字符串“同构”的技巧 题目大意 给定A,B两个序列,要求B在A中出现的次数以及位置.定义字符变换:把所有相同的字符变为另一种字符:两个字符串相等:当且仅当一个字符串可以在若干次字符变换之后变为另一 ...
 - [CODEVS] 3955 最长严格上升子序列(加强版)
			
题目描述 Description 给一个数组a1, a2 ... an,找到最长的上升降子序列ab1<ab2< .. <abk,其中b1<b2<..bk. 输出长度即可. ...
 - 【OS_Linux】Linux中虚拟机的三种上网方式——桥接、NAT、Host-only
			
1.桥接 桥接方便做实验,配置ip方便.可以和局域网中的其他机器进行通信,也可以和公网进行通信.缺点是会占用主机所在局域网的一个ip. 2. NAT NAT模式下虚拟机可以和主机进行通信,可以上网,而 ...
 - 蓝牙学习 (7) - raspberrryPi 扫描TI sensorTag
			
前面几篇分别简单涉及了 raspberryPi上bluez BLE sniffer TI SensorTag https://blog.csdn.net/feiwatson/article/detai ...
 - 《linux设备驱动开发详解》笔记——11内存与IO访问
			
内存访问与映射是linux驱动常见操作,操作硬件时离不开内存的映射,本章比较重要. 11.1 CPU与内存.I/O 目前的嵌入式处理器,都不提供专门的I/O空间,而仅存在内存空间:各种外设寄存器都直接 ...