[Poi2011]Dynamite

Time Limit: 30 Sec  Memory Limit: 128 MB
Submit: 270  Solved: 138
[Submit][Status][Discuss]

Description

The Byteotian Cave is composed of  n chambers and n-1 corridors that connect them. For every pair of chambers there is unique way to move from one of them to another without leaving the cave. Dynamite charges are set up in certain chambers. A fuse is laid along every corridor. In every chamber the fuses from the adjacent corridors meet at one point, and are further connected to the dynamite charge if there is one in the chamber. It takes exactly one unit of time for the fuse between two neighbouring chambers to burn, and the dynamite charge explodes in the instant that fire reaches the chamber it is inside.
We would like to light the fuses in some m chambers (at the joints of fuses) in such a way that all the dynamite charges explode in the shortest time possible since the fuses are lit. Write a program that will determine the minimum such time possible.
 
Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了bomb,现在需要点燃M个点上的引线引爆所有的bomb。
某个点上的引线被点燃后的1单位时间内,在树上和它相邻的点的引线会被点燃。如果一个有bomb的点的引信被点燃,那么这个点上的bomb会爆炸。
求引爆所有bomb的最短时间。 输入:
第一行是两个整数N,M。(1<=m<=n<=300000)
接下来一行有N个整数Di,第I个数为1表示该点有bomb。
接下来N-1行每行有两个数A,B,表示A和B之间有一条边。
输出:
最短时间。
样例解释: 
点燃3,5上的引线。

Input

The first line of the standard input holds two integers n and m (1<=M<=N<=300000)
, separated by a single space, that denote, respectively, the number of chambers in the cave and the number of chambers in which fire can be set to the fuses. The chambers are numbered from 1 to n . The next line contains  n integers d1,d2…dn (Di属于{0,1}, separated by single spaces. If Di=1 , then there is dynamite in the -th chamber, and if di=0 , there is none. The following n -1 lines specify the corridors of the cave. Each of them holds two integers a,b (a<=a<b<=n), separated by a single space, denoting that there is a corridor connecting the chambers a and b . Every corridor appears exactly once in the description.
You may assume that in tests worth 10% of the points it holds additionally that n<= 10, while in tests worth 40% of the points it holds that N<=1000.

Output

The first and only line of the standard output should hold a single integer, equal to the minimum time it takes from lighting the fuses to the explosion of all the charges.

Sample Input

7 2
1 0 1 1 0 1 1
1 3
2 3
3 4
4 5
5 6
5 7

Sample Output

1

HINT

https://blog.csdn.net/PoPoQQQ/article/details/46389701
 
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 300100
using namespace std;
int n,m;
int flag[N];
int head[N];
int status[N];
int document[N];
int cnt;
int tot;
struct node
{
int from,to,next;
}edge[N<<];
void init()
{
memset(head,-,sizeof(head));
cnt=;
}
void edgeadd(int from,int to)
{
edge[cnt].from=from,edge[cnt].to=to;
edge[cnt].next=head[from];
head[from]=cnt++;
}
// 0-> 继续传递
// 1-> 有未被覆盖
// 2-> 既没有传递也没有未被覆盖。
void dfs(int now,int fa,int dis)
{
int uncovered=flag[now]-;
int near=-;
for(int i=head[now];i!=-;i=edge[i].next)
{
int to=edge[i].to;
if(to==fa)continue;
dfs(to,now,dis);
}
for(int i=head[now];i!=-;i=edge[i].next)
{
int to=edge[i].to;
if(to==fa)continue;
if(status[to]==)
near=max(near,document[to]-);
else if(status[to]==)uncovered=max(uncovered,document[to]+);
}
if(near<uncovered)
{
if(uncovered==dis)
document[now]=dis,status[now]=,tot++;
else document[now]=uncovered,status[now]=;
}else if(near!=-)document[now]=near,status[now]=;
else status[now]=,document[now]=;
}
bool check(int dis)
{
tot=;
dfs(,,dis);
if(status[]==)tot++;
return tot<=m?:;
}
int main()
{
init();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&flag[i]);
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
edgeadd(x,y);
edgeadd(y,x);
}
int l=,r=n,ans;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid))ans=mid,r=mid-;
else l=mid+;
}
printf("%d\n",ans);
}

bzoj 2525 [Poi2011]Dynamite 二分+树形dp的更多相关文章

  1. 【BZOJ2525】[Poi2011]Dynamite 二分+树形DP

    [BZOJ2525][Poi2011]Dynamite Description Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了炸.药,现在需要点燃M个点上的引线引爆所有 ...

  2. BZOJ 2525 Poi2011 Dynamite 二分答案+树形贪心

    题目大意:给定一棵树,有一些点是关键点,要求选择不超过mm个点.使得全部关键点到近期的选择的点距离最大值最小 二分答案,问题转化为: 给定一棵树,有一些点是关键点,要求选择最少的点使得每一个关键点到选 ...

  3. Bzoj 2525 [Poi2011]Dynamite

    2525: [Poi2011]Dynamite Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 240  Solved: 120[Submit][Sta ...

  4. bzoj 2525: [Poi2011]Dynamite【二分+树上贪心】

    一眼二分.然后重点是树上贪心部分 长得像dp一样,设mn为子树内已炸点的最浅点,mx为子树内没有炸并且需要炸的最深点,然后转移直接从子树继承即可 然后是判断当前u点是否需要炸,当mx[u]+mn[u] ...

  5. [BZOJ 4033] [HAOI2015] T1 【树形DP】

    题目链接:BZOJ - 4033 题目分析 使用树形DP,用 f[i][j] 表示在以 i 为根的子树,有 j 个黑点的最大权值. 这个权值指的是,这个子树内部的点对间距离的贡献,以及 i 和 Fat ...

  6. 【bzoj5174】[Jsoi2013]哈利波特与死亡圣器 二分+树形dp

    题目描述 给你一棵以1为根的有根树,初始除了1号点为黑色外其余点均为白色.Bob初始在1号点.每次Alice将其中至多k个点染黑,然后Bob移动到任意一个相邻节点,重复这个过程.求最小的k,使得无论B ...

  7. [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩)

    [BZOJ 4455] [ZJOI 2016] 小星星 (树形dp+容斥原理+状态压缩) 题面 给出一棵树和一个图,点数均为n,问有多少种方法把树的节点标号,使得对于树上的任意两个节点u,v,若树上u ...

  8. 【题解】hdu 3586 Information Disturbing 二分 树形dp

    题目描述 Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java ...

  9. 【BZOJ2525】[Poi2011]Dynamite(二分,树形dp)

    [BZOJ2525][Poi2011]Dynamite Description Byteotian Cave的结构是一棵N个节点的树,其中某些点上面已经安置了炸.药,现在需要点燃M个点上的引线引爆所有 ...

随机推荐

  1. 【Leetcode】Jewels and Stones

    Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...

  2. List中的FindAll用法

    在泛型List中查找符合某个字段的全部数据,可以采用如下方式: //1.现将实体数据listList<ADDaAn> objDAList = db.ADDaAns.ToList(); // ...

  3. Spring AOP(一)——基础概念

    前文的一些内容更多是针对Spring容器内部的一些特性的描述,接下来一个专题将描述Spring AOP的一些信息,配置细节等等. 介绍 面向切面编程(AOP)是一种新的针对程序结构的思路,它补足了面向 ...

  4. elasticsearch 关联查询

    父-子关系文档 父-子关系文档 在实质上类似于 nested model :允许将一个对象实体和另外一个对象实体关联起来. 而这两种类型的主要区别是:在 nested objects 文档中,所有对象 ...

  5. 【数据库】 SQL 使用注意点

    [数据库] SQL 使用注意点 一. 索引 1. 常用的搜索条件,都建议加上索引,但状态列除外(该列只有0,1或几个值,不需要加索引,因为没效果) 2. 查询时, 索引列不能做函数处理,会不走索引 3 ...

  6. Django笔记 —— 模型高级进阶

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  7. OpenCV入门:(六:基础画图函数)

    有时程序中需要画一些基础的图形,例如直线,矩形,椭圆以及多边形.OpenCV中当然有此类函数. 1.函数介绍 直线line: , , ) img – 图像 pt1 – 直线起点 pt2 – 直线终点 ...

  8. CSP201409-1:相邻数对

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  9. mysql 连接问题

    用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). MySQL版 ...

  10. Spark实战练习03--Pair RDD

    一.场景 现有某网站的网站日志,内容为用户对网站的请求,包含user ID.IP address.datetime……等等 另有一份文件中包含用户的账户详细信息数据,包含User ID.creatio ...