洛谷—— P3576 [POI2014]MRO-Ant colony

https://www.luogu.org/problem/show?pid=3576

题目描述

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 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 ight floorm/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(2le n,gle 1 000 0002n,g1 000 000, 1le kle 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​​ (1le m_ile 10^91mi​​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-1n1 lines that follow describe the corridors within the ant hill;the ii-th such line contains two integers a_iai​​,b_ibi​​ (1le a_i,b_ile n1ai​​,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.

输入输出样例

输入样例#1:
7 5 3
3 4 1 9 11
1 2
1 4
4 3
4 5
4 6
6 7
输出样例#1:
21

说明

给一棵树,对于每个叶子节点,都有g群蚂蚁要从外面进来,每群蚂蚁在行进过程中只要碰到岔路,就将平均地分成岔路口数-1那么多份,然后平均地走向剩下的那些岔路口,余下的蚂蚁自动消失,树上有一个关键边,假如有一群蚂蚁通过了这条边且数量恰好为k,这k只蚂蚁就被吃掉,问一共有多少只蚂蚁被吃掉

从关键路的端点DFS,统计出到达每个点的最大和最小的蚂蚁数,(只有当最小值不比最大的蚁群数时,才继续搜下一层)

二分统计出每个点能得到的最大蚁群数、

 1 #include <algorithm>
 2 #include <cstdio>
 3 
 4 #define LL long long
 5 const int N(1000005);
 6 inline void read(LL &x)
 7 {
 8     x=0; register char ch=getchar();
 9     for(; ch>'9'||ch<'0'; ) ch=getchar();
10     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
11 }
12 LL n,g,k,s1,s2,gi[N];
13 int head[N],sumedge;
14 struct Edge {
15     int v,next;
16     Edge(int v=0,int next=0):v(v),next(next){}
17 }edge[N<<1];
18 inline void ins(int u,int v)
19 {
20     edge[++sumedge]=Edge(v,head[u]);
21     head[u]=sumedge;
22     edge[++sumedge]=Edge(u,head[v]);
23     head[v]=sumedge;
24 }
25 
26 #define min(a,b) (a<b?a:b)
27 #define max(a,b) (a>b?a:b)
28 int du[N],dad[N],minn[N],maxx[N];
29 void DFS(int u)
30 {
31     for(int v,i=head[u]; i; i=edge[i].next)
32     {
33         v=edge[i].v;
34         if(dad[u]==v) continue;
35         dad[v]=u; du[u]++;
36     }
37     for(int v,i=head[u]; i; i=edge[i].next)
38     {
39         v=edge[i].v;
40         if(dad[u]==v) continue;
41         minn[v]=minn[u]*du[u];
42         maxx[v]=(maxx[u]+1)*du[u]-1;
43         maxx[v]=min(maxx[v],gi[g]);
44         if(minn[v]<=gi[g]) DFS(v);
45     }
46 }
47 
48 LL l,r,mid,ans;
49 LL check(LL x)
50 {
51     LL ret=0;
52     for(l=1,r=g; l<=r; )
53     {
54         mid=l+r>>1;
55         if(gi[mid]<x)
56         {
57             ret=mid;
58             l=mid+1;
59         }
60         else r=mid-1;
61     }
62     return ret;
63 }
64 
65 int Presist()
66 {
67     read(n),read(g),read(k);
68     for(int i=1; i<=g; ++i) read(gi[i]);
69     read(s1);read(s2);
70     for(LL u,v,i=2; i<n; ++i)
71         read(u),read(v),ins(u,v);
72     std::sort(gi+1,gi+g+1);
73     maxx[s1]=maxx[s2]=minn[s1]=minn[s2]=k;
74     DFS(s1); DFS(s2);
75     for(int i=1; i<=n; ++i)
76         if(!du[i]) ans+=check(maxx[i]+1)-check(minn[i]);
77     printf("%lld
",ans*k);
78     return 0;
79 }
80 
81 int Aptal=Presist();
82 int main(){;}
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/7523487.html