[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. C++常量(const)的使用

    #include <iostream> using namespace std; class MyClass { public: int GetValue() const ; int Ge ...

  2. 嵌入式框架Zorb Framework搭建三:列表的实现

    我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...

  3. springmvc springboot 跨域问题(CORS)

    官方文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html springmvc s ...

  4. 8 TFTP代码详解 协议写在程序中

    1.版本1:发送请求 # -*- coding:utf-8 -*- import struct from socket import * #0. 获取要下载的文件名字: downloadFileNam ...

  5. P2347 砝码称重

    P2347 砝码称重 题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入输出格式 输入格式: 输入方式:a1 a2 a3 a4 a5 a6 (表示1 ...

  6. VC++之运算符重载简单小结

    封装继承和多态是面向对象三大基本支柱.在面向对象系统中有两种编译方式:静态联编和动态联编静态联编:也叫早期联编:指系统在编译时就决定如何实现某一动作,提供了执行速度快的优点.动态联编:也叫滞后联编:指 ...

  7. Python参考

    python中os模块用法 自动化运维Python系列(五)之常用模块 最常用的Notepad++的快捷键 pycharm快捷键 最全Pycharm教程(1)——定制外观 pycharm教程大全 py ...

  8. Remix-Solidity IDE上run选项下Environment选择Web3 Provider,出现不能连接到测试网Ganache提示

    解决办法:通常情况下,自己使用的浏览器IDE是:https://ethereum.github.io/browser-solidity,如果出现连接不到Ganache测试网的提示,可以使用另一种浏览器 ...

  9. linux中升级安装python2.7

    打算自建VPN,新购买了一个虚拟服务器,centOS6.6 自带的是python2.6,因为比较习惯python2.7,所以就升级到最新的python2.7.12 首先要安装:sudo yum ins ...

  10. 软工实践Beta冲刺(7/7)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...