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 n

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 g

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

• 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

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

We want to know how many ants the anteater will eat.

## 输入输出格式

The first line of the standard input contains three integers n

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

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


## 说明

分析：

一道比较考思维的题。

如果按照题目的要求从叶子节点开始做的话，很难有比较优秀的方法。

那么就换一种方式，从两个给定的根开始。因为给定的一条边一定会把一棵树分割成两部分，所以我们可以直接把这棵树当作两棵树来处理。对于每一棵树，从根节点开始$DFS$，确定从这个点到达根节点时如果要正好有$k$只蚂蚁，在这个点至少需要多少蚂蚁，至多能有多少蚂蚁。再对给定的每一群蚂蚁排序。处理完以后，对于所有的叶子节点二分答案找到每个叶子节点有多少群蚂蚁合法，最后输出答案就行了。

讲的比较抽象，可以看代码理解。

Code：

//It is made by HolseLee on 5th Nov 2018
//Luogu.org P3576
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N=1e6+7;
ll ans,maxn[N],minn[N],c[N];
struct Edge { int to,nxt; }e[N<<1];

{
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;
}

{
}

void dfs(int x)
{
if( e[i].to!=fa[x] ) {
fa[e[i].to]=x; dg[x]++;
}
}
y=e[i].to;
if( y==fa[x] ) continue;
minn[y]=minn[x]*dg[x];
maxn[y]=(maxn[x]+1)*dg[x]-1;
maxn[y]=min(maxn[y],c[g]);
if( minn[y]<=c[g] ) dfs(y);
}
}

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

int main()
{
sort(c+1,c+g+1);
for(int i=2; i<n; ++i) {
}