2525: [Poi2011]Dynamite

Time Limit: 30 Sec  Memory Limit: 128 MB
Submit: 240  Solved: 120
[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个节点的树,其中某些点上面已经安置了炸 药,现在需要点燃M个点上的引线引爆所有的炸 药。
某个点上的引线被点燃后的1单位时间内,在树上和它相邻的点的引线会被点燃。如果一个有炸 药的点的引信被点燃,那么这个点上的炸 药会爆炸。
求引爆所有炸 药的最短时间。 输入:
第一行是两个整数N,M。(1<=m<=n<=300000)
接下来一行有N个整数Di,第I个数为1表示该点有炸 药。
接下来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
  这道题还算比较水,上来以为所有点都放了炸 弹,那么果断二分答案+贪心啊。然而看到不是每个点都有炸弹后感觉这个人都不好了……
  然后果断重新想,发现二分答案还是可行的,仍然是尽可能的不到不点不行的时候点……
  然后又开始继续打码,好不容易打完了又发现自己打的还是错的,心痛……
  然而再接再厉,接着去分析。我们把所有节点分为3类。第一类,下面全是引线,没有炸弹。那么,我们可以直接忽略它。第二种,底下是炸弹,并且没有被点燃,这时,我们就要把它的子树中的离当前节点最远的状态传上来了。第三种,就是下面已经有引线被点燃,而且它有可能接着在我们规定的时间内点燃其他炸弹,与第二种相反,我们要找的是离当前节点最近的。
  在我们搜到一个点后,我们统计来自他子树的答案,如果下面都是引线,那么这个节点的状态就看他是不是炸弹了。如果他下面只有被点燃的引线,我们找到离他最近的引线转移过去,当然,如果他与引线的距离远,我们就没必要传递了。如果下面只有炸弹,我们就要看一下最远的炸弹离他是多远了。如果比当前值小,我们就直接传下去就行,否则将该点点燃,然后将状态改为点燃引线,接着传递。
  比较复杂的是下面既有被点燃的引线又有炸弹。我们需要分类讨论一下,如果引线可以点燃炸弹就把他的状态定为被点燃的引线,否则就改为炸弹,记得在这里也判断一下炸弹是否需要点燃,否则WA到死。最后判断一下根节点是什么状态,如果是炸弹我们还是要再点一个的。
 #include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define N 300005
using namespace std;
int n,a[N],zz,m;
struct no
{
int to,next;
}road[N*];
void build(int x,int y)
{
zz++;
road[zz].to=y;
road[zz].next=a[x];
a[x]=zz;
}
int bom[N];
int size[N],fa[N];
void dfs1(int x)
{
size[x]=;
for(int i=a[x];i>;i=road[i].next)
{
int y=road[i].to;
if(y==fa[x])continue;
fa[y]=x;
dfs1(y);
size[x]+=size[y];
}
}
int sum,f[N],c[N];
void dfs(int x,int t)
{
if(size[x]==)
{
if(bom[x])
{
c[x]=;
f[x]=;
if(t==)
{
sum++;
f[x]=;
c[x]=;
}
}
else
{
c[x]=-;
f[x]=-;
}
return;
}
int mn=0x7fffffff,mx=-;
bool yx=;
for(int i=a[x];i>;i=road[i].next)
{
int y=road[i].to;
if(y==fa[x])continue;
dfs(y,t);
if(c[y]==-)continue;
yx=;
if(c[y]==)
{
if(f[y]+<=t) mn=min(mn,f[y]+);
} else
mx=max(mx,f[y]+);
}
if(yx)
{
c[x]=-;f[x]=-;
if(bom[x])
{
c[x]=;
f[x]=;
if(t==)
{
sum++;
c[x]=;
}
}
return;
}
if(mx==-&&mn!=0x7fffffff)
{
c[x]=;
f[x]=mn;
return;
}
if(mx==-&&mn==0x7fffffff)
{
if(bom[x])
{
c[x]=;
f[x]=;
if(t==)
{
sum++;
f[x]=;
c[x]=;
}
}
else
{
c[x]=-;
f[x]=-;
}
return;
}
if(mn==0x7fffffff)
{
if(mx>=t)
{
sum++;
f[x]=;
c[x]=;
}
else
{
c[x]=;
f[x]=mx;
}
return;
}
if(mn+mx<=t)
{
c[x]=;
f[x]=mn;
return;
}
if(mx>=t)
{
sum++;
f[x]=;
c[x]=;
}
else
{
c[x]=;
f[x]=mx;
}
}
bool check(int k)
{
memset(c,,sizeof(c));
memset(f,,sizeof(f));
sum=;
dfs(,k);
if(c[]==)sum++;
return sum<=m;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&bom[i]);
}
for(int i=;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
build(x,y);
build(y,x);
}
dfs1();
int li=,ri=n,mid;
while(li<=ri)
{
mid=(li+ri)>>;
if(check(mid)) ri=mid-;
else li=mid+;
}
printf("%d\n",ri+);
return ;
}

Bzoj 2525 [Poi2011]Dynamite的更多相关文章

  1. bzoj 2525 [Poi2011]Dynamite 二分+树形dp

    [Poi2011]Dynamite Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 270  Solved: 138[Submit][Status][D ...

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

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

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

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

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

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

  5. BZOJ 2530 Poi2011 Party 【枚举】

    BZOJ 2530 Poi2011 Party Description Byteasar intends to throw up a party. Naturally, he would like i ...

  6. [bzoj 2216] [Poi2011] Lightning Conductor

    [bzoj 2216] [Poi2011] Lightning Conductor Description 已知一个长度为n的序列a1,a2,-,an. 对于每个1<=i<=n,找到最小的 ...

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

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

  8. [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】

    题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...

  9. [BZOJ 2350] [Poi2011] Party 【Special】

    题目链接: BZOJ - 2350 题目分析 因为存在一个 2/3 n 大小的团,所以不在这个团中的点最多 1/3 n 个. 牺牲一些团内的点,每次让一个团内的点与一个不在团内的点抵消删除,最多牺牲 ...

随机推荐

  1. 零元学Expression Blend 4 - Chapter 16 用实例了解互动控制项「Button」II

    原文:零元学Expression Blend 4 - Chapter 16 用实例了解互动控制项「Button」II 本章将教大家如何制作自己的Button,并以玻璃质感Button为实作案例. ? ...

  2. javascript学习路线图

    史上最全的javascript学习路线图 JavaSctipt学习路线 完成整个课程大纲需要花上6~8周的时间,将学会完整的JavaScript语言(包括jQuery和一些HTML5).如果你没有时间 ...

  3. 【Qt】无边框窗体中带有ActiveX组件时的一个BUG

    无意中发现的一个BUG,Qt5.1.1正式版首先创建一个GUI工程,拖入一个QAxWidget控件(为了使ActiveX生效,需要在.pro文件中加入CONFIG += qaxcontainer)接着 ...

  4. 一条命令,秒秒钟完成MD5、SHA1校验,这就叫效率!

    相信很多奋斗在运维战线的小伙伴们经常会遇到版本升级之类的问题.笔者之前所在的公司每次进行版本发布的时候都会附带MD5校验哈希值,每次升级之前一般都要核对MD5哈希值的,刚刚开始的时候对Linux并不是 ...

  5. 海康威视频监控设备Web查看系统(一):概要篇

    声明:本系列文章只提供交流与学习使用.文章中所有涉及到海康威视设备的SDK均可在海康威视官方网站下载得到.文章中所有除官方SDK意外的代码均可随意使用,任何涉及到海康威视公司利益的非正常使用由使用者自 ...

  6. 学习Java,值得你留意的问题(1)更名为《学习Java,容易被你忽略的小细节(1)》

    记得大二快要结束的时候,有个女孩子突然问我“你会Java吗,帮我做大作业好吗?” 实话说,那个女孩真的很漂亮,我当时也非常想帮她.但是我从来没有接触过Java,让我在短短的几天内完成Java程序设计课 ...

  7. c++类运算符重载遇到的函数形参问题

    class A { public: A(int arg1, int arg2); ~A(); A &operator = ( A &other); A operator + ( A & ...

  8. Spring之bean后处理器

    Bean后处理器是一种特殊的Bean,容器中所有的Bean在初始化时,均会自动执行该类的两个方法.由于该Bean是由其它Bean自动调用执行,不是程序员手工调用,故此Bean无须id属性.需要做的是, ...

  9. 线性表List

    数组array是基本的数据结构,但它的功能有限,线性表list可以认为是扩展了功能的数组.可以自动调整大小.添加和删除元素不需要其他元素移位. 根据指针数量和指向的不同,线性表分为单向链表.双向链表和 ...

  10. springboot 集成完整的swagger2

    springboot 在集成swagger中会不会遇到各种问题: 1.swagger  进行接口鉴权(比如设置header的token,接口进行拦截处理). 2.swagger 进行实体属性解析(po ...