洛谷—— 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-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_gm?1??,m?2??,?,m?g?? 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≤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 nn.

The second line contains gg integers m_1,m_2,\cdots,m_gm?1??,m?2??,?,m?g?? (1\le m_i\le 10^91≤m?i??≤10?9??), separated by single spaces, where m_im?i?? 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_ia?i??,b_ib?i?? (1\le a_i,b_i\le n1≤a?i??,b?i??≤n), separated by a single space, that indicate that the chambers no. a_ia?i?? and b_ib?i?? 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\n",ans*k);
78     return 0;
79 }
80
81 int Aptal=Presist();
82 int main(){;}
时间: 09-15

洛谷—— 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. 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

洛谷 P2709 BZOJ 3781 小B的询问

题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小B请你帮助他回答询问. 输入输出格式 输入格式: 第一行,三个整数N.M.K. 第二行,N个整数,表示小B的序列. 接下来的M行,每行两个整数L.R. 输出格式: M行,每行一个整数,其中第i行的整数表示第i个询问的答案. 输入输出样例 输入样例#1: 6 4 3 1 3 2 1 1 3

洛谷1231 教辅的组成

洛谷1231 教辅的组成 https://www.luogu.org/problem/show?pid=1231 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书多得数不胜数,其中有书,有答案,有练习册.已知一个完整的书册均应该包含且仅包含一本书.一本练习册和一份答案,然而现在全都乱做了一团.许多书上面的字迹都已经模糊了,然而HansBug还是可

洛谷教主花园dp

洛谷-教主的花园-动态规划 题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树的高度分别为10,20,30.教主希望这一圈树种得有层次感,所以任何一个位置的树要比它相邻的两棵树的高度都高或者都低,并且在此条件下,教主想要你设计出一套方案,使得观赏价值之和最高. 输入输出格式 输入格式: 输入文件garden.in的第1行为一个正整数n,表示需要种的

洛谷 P2801 教主的魔法 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2801 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始都是不超过1000的正整数.教主的魔法每次可以把闭区间[L, R](1≤L≤R≤N)内的英雄的身高全部加上一个整数W.(虽然L=R时并不

洛谷P1466 集合 Subset Sums

洛谷P1466 集合 Subset Sums这题可以看成是背包问题 用空间为 1--n 的物品恰好填充总空间一半的空间 有几种方案 01 背包问题 1.注意因为两个交换一下算同一种方案,所以最终 要 f [ v ] / 2 2.要开 long long 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string&g

洛谷P1160 队列安排 链表

洛谷P1160 队列安排   链表 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdlib> 5 #include <string> 6 #include <algorithm> 7 #include <iomanip> 8 #include <iostream> 9 using namespace std

洛谷 P3367 并查集模板

#include<cstdio> using namespace std; int n,m,p; int father[2000001]; int find(int x) { if(father[x]!=x) father[x]=find(father[x]); return father[x]; } void unionn(int i,int j) { father[j]=i; } int main() { scanf("%d%d",&n,&m); for

[题解]洛谷比赛『期末考后的休闲比赛2』

[前言] 这场比赛已经结束了有几天,但我各种忙,虽然AK但还是没来得及写题解.(我才不会告诉你我跑去学数据结构了) T1 区间方差 (就不贴题好了) 首先可以推公式(我们可以知道,线段树然而并不能通过初中学过的方差公式在log(L)内求出方差): (s2表示方差,L表示区间长度,xi表示区间的每一项,最后一个x上画了一根线表示这些数据的平均数) 用二项式定理完全平方公式可得: 再次展开: 另外,再代入以下这个 得到了: 然后继续吧.. 然后duang地一声合并同类项,于是我们得到了: 然后可以高