[BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony
试题描述

输入
输出
输入示例
输出示例
数据规模及约定
见“输入”
题解
因为只有一只食蚁兽,所以可以从它所在的边开始 BFS,求出每条边经过的蚂蚁上限和下限,扩展到叶节点后二分找有几群蚂蚁在下界和上界范围内累计答案即可。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 1000010
#define maxm 2000010
#define LL long long
int n, g, k, m, head[maxn], next[maxm], to[maxm], ind[maxn];
LL ants[maxn], mxa; void AddEdge(int a, int b) {
to[++m] = b; next[m] = head[a]; head[a] = m;
swap(a, b);
to[++m] = b; next[m] = head[a]; head[a] = m;
return ;
} bool vis[maxn];
int Q[maxn], hd, tl;
LL low[maxn], upp[maxn];
void BFS(int s) {
hd = tl = 0; Q[++tl] = s;
while(hd < tl) {
int u = Q[++hd];
// printf("%d %lld %lld\n", u, low[u], upp[u]);
for(int e = head[u]; e; e = next[e]) if(!vis[to[e]]) {
Q[++tl] = to[e]; vis[to[e]] = 1;
if(low[u] * ((LL)ind[u] - 1) > mxa) low[to[e]] = mxa + 1;
else low[to[e]] = low[u] * ((LL)ind[u] - 1);
if(upp[u] * ((LL)ind[u] - 1) + ind[u] - 2 > mxa) upp[to[e]] = mxa + 1;
else upp[to[e]] = upp[u] * ((LL)ind[u] - 1) + ind[u] - 2;
}
}
return ;
} int main() {
// freopen("data.in", "r", stdin);
// freopen("data.out", "w", stdout);
n = read(); g = read(); k = read();
for(int i = 1; i <= g; i++) ants[i] = read(), mxa = max(mxa, ants[i]); int sta, stb;
for(int i = 1; i < n; i++) {
int a = read(), b = read();
AddEdge(a, b);
ind[a]++; ind[b]++;
if(i == 1) sta = a, stb = b;
}
vis[sta] = vis[stb] = 1;
low[sta] = low[stb] = upp[sta] = upp[stb] = (LL)k;
BFS(sta);
BFS(stb); sort(ants + 1, ants + g + 1);
LL ans = 0;
// for(int i = 1; i <= n; i++) if(ind[i] == 1) printf("%d: %lld %lld\n", i, low[i], upp[i]);
for(int i = 1; i <= n; i++) if(ind[i] == 1) {
int l = lower_bound(ants + 1, ants + g + 1, low[i]) - ants;
int r = upper_bound(ants + 1, ants + g + 1, upp[i]) - ants;
if(r > g || ants[r] > upp[i]) r--;
ans += (LL)(r - l + 1);
// printf("leaves: %d %d %d\n", i, l, r);
}
ans *= (LL)k;
printf("%lld", ans); return 0;
}
[BZOJ3872][Poi2014]Ant colony的更多相关文章
- $bzoj3872\ [Poi2014]\ Ant\ colony$ 二分+$dp$
正解:二分+$dp$ 解题报告: 传送门$QwQ$ 一年过去了依然没有头绪,,,$gql$的$NOIp$必将惨败了$kk$. 考虑倒推,因为知道知道除数和答案,所以可以推出被除数的范围,然后一路推到叶 ...
- 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分
[BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...
- 【BZOJ3872】Ant colony(二分,动态规划)
[BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...
- bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分
3872: [Poi2014]Ant colony Time Limit: 30 Sec Memory Limit: 128 MB Description There is an entranc ...
- [bzoj3872][Poi2014]Ant colony_树形dp
Ant colony bzoj-3872 Poi-2014 题目大意:说不明白.....题目链接 注释:略. 想法:两个思路都行. 反正我们就是要求出每个叶子节点到根节点的每个路径权值积. 可以将边做 ...
- [POI2014]Ant colony
题目大意: 给定一棵$n(n\le10^6)$个结点的树.在每个叶子结点,有$g$群蚂蚁要从外面进来,其中第$i$群有$m_i$只蚂蚁.这些蚂蚁依次爬树(一群蚂蚁爬完后才会爬另一群),若当前经过结点度 ...
- bzoj 3872 [Poi2014]Ant colony——二分答案
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3872 可以倒推出每个叶子节点可以接受的值域.然后每个叶子二分有多少个区间符合即可. 注意一开 ...
- bzoj 3872: [Poi2014]Ant colony【树形dp+二分】
啊我把分子分母混了WA了好几次-- 就是从食蚁兽在的边段成两棵树,然后dp下去可取的蚂蚁数量区间,也就是每次转移是l[e[i].to]=l[u](d[u]-1),r[e[i].to]=(r[u]+1) ...
- Luogu3576 POI2014 MRO-Ant colony 【树形DP】*
Luogu3576 POI2014 MRO-Ant colony The ants are scavenging an abandoned ant hill in search of food. Th ...
随机推荐
- Git.Framework 框架随手记--ORM查询返回实体对象
使用ORM有一个优势,可以通过某种机制将数据库中的数据转化为自己想要的对象形式数据.本章记录一下如何使用Git.Framework返回实体对象 一. Git.Framework 中提供的方法 在Git ...
- SQL修改表结构之添加主键,添加IDENTITY属性
设计一张表时没有考虑到主键Id及自增长,现又需要,原脚本: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[F ...
- NABC竞争性需求分析
设计一个五子棋游戏 下面是比较系统的框架-NABC模型 1) N (Need 需求) 现在随着人们的生活越来越好,电脑已经成为每家每户的必备品了,而且很多人工作的地方都也是必备的电脑,而 ...
- codevs 1690 开关灯 线段树水题
没什么好说的,标记put表示开关是否开着. #include<cstdio> #include<cstring> #include<algorithm> using ...
- BZOJ-1407 Savage 枚举+拓展欧几里得(+中国剩余定理??)
zky学长实力ACM赛制测试,和 大新闻(YveH) 和 华莱士(hjxcpg) 组队...2h 10T,开始 分工我搞A,大新闻B,华莱士C,于是开搞: 然而第一题巨鬼畜,想了40min发现似乎不可 ...
- on the way to Peking University
明天就要去北京参加北大夏令营了,希望这次能有所斩获! on the way to Peking University
- 802.11协议帧格式、Wi-Fi连接交互过程、无线破解入门研究
相关学习资料 Linux黑客大曝光: 第8章 无线网络 无线网络安全攻防实战进阶 无线网络安全 黑客大曝光 第2版 http://zh.wikipedia.org/wiki/IEEE_802.11 h ...
- JSP登录验证并显示信息
加入C标签: 加入jstl.jar 和standard.jar加入Lib文件夹中 将c.tld放入WEB-Info文件夹中 index.jsp <%@ page language="j ...
- 单调队列 I
2009国家集训队徐持衡的论文<浅谈几类背包问题>里提到的一个经典问题: 长度限制最大连续和问题: 给出长度为 n 的序列 X i ,求这个序列中长度不超过 Lmax 的最大连续和. Im ...
- P、NP、NPC、NP-Hard问题
转自:http://www.matrix67.com/blog/archives/105 总结 P:能用多项式时间求解的问题NP:能用多项式时间验证的问题(但不知道能不能用多项式时间求解).存在不属于 ...