Evanyou Blog 彩带

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


MRO-Ant colony


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

The ant hill has nn chambers and n-1n1 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 \rfloorm/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.





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










//It is made by HolseLee on 5th Nov 2018
//Luogu.org P3576
using namespace std;

typedef long long ll;
const int N=1e6+7;
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<<1];

inline int read()
    char ch=getchar(); int x=0; bool flag=false;
    while( ch<'0' || ch>'9' ) {
        if( ch=='-' ) flag=true; ch=getchar();
    while( ch>='0' && ch<='9' ) {
        x=x*10+ch-'0'; 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) {
        if( y==fa[x] ) continue;
        if( minn[y]<=c[g] ) dfs(y);

inline ll getans(ll x)
    int l=0,r=g,mid,ret=0;
    while( l<=r ) {
        if( c[mid]<x ) l=mid+1,ret=mid;
        else r=mid-1;
    return ret;

int main()
    n=read(), g=read(), k=read();
    for(int i=1; i<=g; ++i) c[i]=read();
    int x,y; sx=read(), sy=read();
    for(int i=2; i<n; ++i) {
        x=read(), y=read(); add(x,y);
    for(int i=1; i<=n; ++i)
    if( !dg[i] ) ans+=getans(maxn[i]+1)-getans(minn[i]);
    return 0;


posted @ 2018-11-05 17:06  HolseLee  阅读(291)  评论(0编辑  收藏  举报