bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分
3872: [Poi2014]Ant colony
Time Limit: 30 Sec Memory Limit: 128 MBDescription
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 m1,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 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 floor(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 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组,每组将会有floor(m/d)只,如下图。
![](http://www.lydsy.com/JudgeOnline/upload/201501/s.png)
一只饥饿的食蚁兽埋伏在一条边上,如果有一群蚂蚁通过这条边,并且数量恰为k只,它就会吞掉这群蚂蚁。请计算一共有多少只蚂蚁会被吞掉。
Input
The first line of the standard input contains three integers n, g, k (2<=n,g<=1000000, 1<=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 n.
The second line contains g integers m[1],m[2],...,m[g](1<=m[i]<=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<=a[i],b[i]<=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
3 4 1 9 11
1 2
1 4
4 3
4 5
4 6
6 7
Sample Output
21
HINT
显然得从那条关键边倒着推
以这条边作为这棵树的“根”,开始遍历其他的边,遍历到每条边的时候计算一下“到这条边时这群蚂蚁会被吃掉”的当时蚂蚁数量上限和下限
然后对于每个叶子节点的那些边,二分一下有多少组蚂蚁会被吃掉就好了
#include<map> #include<cmath> #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define ll long long #define N 1000010 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; } ll lj[N],fro[N<<1],to[N<<1],du[N],cnt,s1,s2; inline void add(int a,int b){to[++cnt]=b;fro[cnt]=lj[a];lj[a]=cnt;du[a]++;} ll mn[N],mx[N],n,g,k,m[N],u,v,ans; void dfs(int x,int f) { int v; for(int i=lj[x];i;i=fro[i]) { v=to[i]; if(v==f) continue; mn[v]=mn[x]*(du[x]-1); mx[v]=mx[x]*(du[x]-1)+du[x]-2; mx[v]=min(mx[v],m[g]); if(mn[v]<=m[g]) dfs(v,x); } } ll cal(ll x) { ll L=1,R=g+1,mid; while(L<R) { mid=(L+R)>>1; if(m[mid]<=x) L=mid+1; else R=mid; } return L-1; } int main() { n=read();g=read();k=read(); for(int i=1;i<=g;i++) m[i]=read(); sort(m+1,m+g+1); s1=read();s2=read(); add(s1,s2);add(s2,s1); for(int i=2;i<n;i++) { u=read();v=read(); add(u,v);add(v,u); } mn[s1]=mn[s2]=mx[s1]=mx[s2]=k; dfs(s1,s2);dfs(s2,s1); for(int i=1;i<=n;i++) if(du[i]==1) ans+=cal(mx[i])-cal(mn[i]-1); printf("%lld\n",ans*k); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。