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]\)要满足条件就需要满足所有前缀和 ...
随机推荐
- reorder list(链表重新排序)
Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do thi ...
- 排序算法入门之希尔排序(java实现)
希尔排序是对插入排序的改进.插入排序是前面元素已经有序了,移动元素是一个一个一次往后移动,当插入的元素比前面排好序的所有元素都小时,则需要将前面所有元素都往后移动.希尔排序有了自己的增量,可以理解为插 ...
- [C#网络应用编程]1、对进程的操作
在.net中,Process类提供了对进程进行管理的各种方法. 一.获取进程集合的方法: Process[] myProcesses = Process.GetProcesses(); //获取本地 ...
- maven常见配置
maven surefire plugin 默认执行失败后,不会继续执行,需要在</configuration>中设置参数 <testFailureIgnore>true< ...
- Mybatis 系列10
在前九篇中,介绍了mybatis的配置以及使用, 那么本篇将走进mybatis的源码,分析mybatis 的执行流程 1. SqlSessionFactory 与 SqlSession. 通过前面的章 ...
- Mac Launchpad出现两个相同快捷方式的解决办法
进入以下目录 ~/Library/Application Support/Dock 把里面的.db文件删掉,然后注销重新登录即可.
- 一天搞懂深度学习-训练深度神经网络(DNN)的要点
前言 这是<一天搞懂深度学习>的第二部分 一.选择合适的损失函数 典型的损失函数有平方误差损失函数和交叉熵损失函数. 交叉熵损失函数: 选择不同的损失函数会有不同的训练效果 二.mini- ...
- php坏境安装Xdebug详情步骤
原创,转载请注明出处! Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况,可以说是程序员必备的一个工具之一.好了,进入正题: 1.下 ...
- 四年级--python函数基础用法
一.函数的定义,调用和返回值 1.1 语法 def 函数(参数一,参数二...): ''' 文档注释 ''' 代码逻辑一 代码逻辑二 .... return 返回值 1.2 定义函数的三种形式 说明: ...
- httpClient连接超时设置
注: 每个HttpClinet对象设置都不一样 这里已3.x和4.x为例说明 1)3.X版本 创建连接 HttpClient httpClient=new DefaultHttpClient(); 这 ...