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

[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
 
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #define N 300100 
 6 using namespace std;
 7 int n,m;
 8 int flag[N];
 9 int head[N];
10 int status[N];
11 int document[N];
12 int cnt;
13 int tot;
14 struct node
15 {
16     int from,to,next;
17 }edge[N<<1];
18 void init()
19 {
20     memset(head,-1,sizeof(head));
21     cnt=1;
22 }
23 void edgeadd(int from,int to)
24 {
25     edge[cnt].from=from,edge[cnt].to=to;
26     edge[cnt].next=head[from];
27     head[from]=cnt++;
28 }
29 // 0-> 继续传递
30 // 1-> 有未被覆盖
31 // 2-> 既没有传递也没有未被覆盖。 
32 void dfs(int now,int fa,int dis)
33 {
34     int uncovered=flag[now]-1;
35     int near=-1;
36     for(int i=head[now];i!=-1;i=edge[i].next)
37     {
38         int to=edge[i].to;
39         if(to==fa)continue;
40         dfs(to,now,dis);
41     }
42     for(int i=head[now];i!=-1;i=edge[i].next)
43     {
44         int to=edge[i].to;
45         if(to==fa)continue;
46         if(status[to]==0)
47             near=max(near,document[to]-1);
48         else if(status[to]==1)uncovered=max(uncovered,document[to]+1); 
49     }
50     if(near<uncovered)
51     {
52         if(uncovered==dis)
53             document[now]=dis,status[now]=0,tot++;
54         else document[now]=uncovered,status[now]=1;
55     }else if(near!=-1)document[now]=near,status[now]=0;
56     else status[now]=2,document[now]=0;
57 }
58 bool check(int dis)
59 {
60     tot=0;
61     dfs(1,0,dis);
62     if(status[1]==1)tot++;
63     return tot<=m?1:0;
64 } 
65 int main()
66 {
67     init();
68     scanf("%d%d",&n,&m);
69     for(int i=1;i<=n;i++)scanf("%d",&flag[i]);
70     for(int i=1;i<n;i++)
71     {
72         int x,y;
73         scanf("%d%d",&x,&y);
74         edgeadd(x,y);
75         edgeadd(y,x);
76     }
77     int l=0,r=n,ans;
78     while(l<=r)
79     {
80         int mid=(l+r)>>1;
81         if(check(mid))ans=mid,r=mid-1;
82         else l=mid+1;
83     }
84     printf("%d\n",ans);
85 } 

 

posted @ 2018-03-31 14:44  Kaiser-  阅读(175)  评论(0编辑  收藏  举报