BZOJ_3872_[Poi2014]Ant colony_dfs
BZOJ_3872_[Poi2014]Ant colony_dfs
Description

Input
Output
Sample Input
3 4 1 9 11
1 2
1 4
4 3
4 5
4 6
6 7
Sample Output
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char nc() {
static char buf[100000],*p1,*p2;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd() {
register int x=0;
register char s=nc();
while(s<'0'||s>'9')s=nc();
while(s>='0'&&s<='9')x=(x<<3)+(x<<1)+s-'0',s=nc();
return x;
}
#define N 1000050
typedef long long ll;
int head[N],to[N<<1],nxt[N<<1],out[N],cnt;
int m[N],n,g,k,root1,root2;
ll upper[N],lower[N];
inline void add(int u,int v) {
to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; out[u]++;
}
void dfs(int x,int y) {
int i;
for(i=head[x];i;i=nxt[i]) {
if(to[i]!=y) {
upper[to[i]]=upper[x]*(out[x]-1)+out[x]-2;
if(upper[to[i]]<0) upper[to[i]]=m[g];
upper[to[i]]=min(upper[to[i]],1ll*m[g]);
lower[to[i]]=lower[x]*(out[x]-1);
if(lower[to[i]]<=m[g]&&lower[to[i]]>=0)
dfs(to[i],x);
}
}
}
int search(ll x) {
int l=1,r=g+1;
while(l<r) {
int mid=(l+r)>>1;
if(m[mid]<=x) l=mid+1;
else r=mid;
}
return l-1;
}
int main() {
n=rd(); g=rd(); k=rd();
int i,x,y;
for(i=1;i<=g;i++) m[i]=rd();
sort(m+1,m+g+1);
root1=rd(); root2=rd();
add(root1,root2); add(root2,root1);
for(i=2;i<n;i++) {
x=rd(); y=rd();
add(x,y); add(y,x);
}
upper[root1]=upper[root2]=lower[root1]=lower[root2]=k;
dfs(root1,root2);
dfs(root2,root1);
ll ans=0;
for(i=1;i<=n;i++) if(out[i]==1) ans+=search(upper[i])-search(lower[i]-1);
printf("%lld\n",ans*k);
}
BZOJ_3872_[Poi2014]Ant colony_dfs的更多相关文章
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- 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+二分
[BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...
- [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) ...
- $bzoj3872\ [Poi2014]\ Ant\ colony$ 二分+$dp$
正解:二分+$dp$ 解题报告: 传送门$QwQ$ 一年过去了依然没有头绪,,,$gql$的$NOIp$必将惨败了$kk$. 考虑倒推,因为知道知道除数和答案,所以可以推出被除数的范围,然后一路推到叶 ...
- POI2014题解
POI2014题解 [BZOJ3521][Poi2014]Salad Bar 把p当作\(1\),把j当作\(-1\),然后做一遍前缀和. 一个合法区间\([l,r]\)要满足条件就需要满足所有前缀和 ...
随机推荐
- 用eclipse运行和部署myeclipse项目
版本:Eclipse-jee-indigo(3.7) 配置Project Facets 点击项目选择 properties -->Project Facets--> Dynamic Web ...
- Delphi 项目总结
Delphi 项目总结 随着项目的失败,这些天一直在总结失败的原因,到底是为什么? 一.技术层面 1.少用指针类型,多用类. 虽然指针类型能有效的节约内 ...
- Git分支创建与合并
分支管理是Git支持多人协作和版本控制的关键,参照廖雪峰对Git的介绍,对真实开发环境中Git的使用结合实践进行梳理. 摘自:廖雪峰的官方网站 在实际开发中,我们应该按照几个基本原则进行分支管理: 首 ...
- PhpStorm服务激活
日期 服务地址 状态 2018-03-15 http://idea.singee77.com/ 使用中
- [INet] WebSocket 协议中的数据收发过程
WebSocket 和 HTTP 相似,只是一个应用层协议,对下层透明,所以不涉及 TCP/IP. 由于浏览器支持了 WebSocket,所以在用 JS 写客户端的时候,是无需考虑数据的编码解码的. ...
- DDGScreenShot — 复杂屏幕截屏(如view ScrollView webView wkwebView)
写在前面 最近有这么一个需求,分享页面,分享的是web订单截图,既然是web 就会有超出屏幕的部分, 生成的图片还要加上我们的二维码,这就涉及到图片的合成了. 有了这样的需求,就是各种google.也 ...
- Yii2表单提交(带文件上传)
今天写一个php的表单提交接口,除了基本的字符串数据,还带文件上传,不用说前端form标签内应该有这些属性 <form enctype="multipart/form-data&quo ...
- hadoop中setup,cleanup,run和context讲解
hadoop 执行中的setup run cleanup context的作用1.简介1) setup(),此方法被MapReduce框架仅且执行一次,在执行Map任务前,进行相关变量或者资源的集中初 ...
- Scrapy爬虫框架第三讲(linux环境)
下面我们来学习下Spider的具体使用: 我们已上节的百度阅读爬虫为例来进行分析: 1 # -*- coding: utf-8 -*- 2 import scrapy 3 from scrapy.li ...
- CSS定位使用方法
.box0 { width: 200px; height: 200px; position: relative; background: #cfa } .box0-1,.box0-2 { width: ...