bzoj 2525: [Poi2011]Dynamite

题意:

某个点上的引线被点燃后的1单位时间内,在树上和它相邻的点的引线会被点燃。如果一个有炸药的点的引信被点燃,那么这个点上的炸药会爆炸。
求引爆所有炸药的最短时间。

题解:

二分+贪心。
关键是想到二分,然后就好做了。
变成已知覆盖范围,问至少要多少个点覆盖所有染色点。
显然贪心是对的,不到临界点不点燃。
code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
int n,m,d[300010];
struct node{
    int y,next;
}a[600010];int len=0,last[300010];
void ins(int x,int y)
{
    a[++len].y=y;
    a[len].next=last[x];last[x]=len;
}
int num;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int f[300010],g[300010];
void dfs(int x,int fa,int t)
{
    if(d[x]) f[x]=0;
    for(int i=last[x];i;i=a[i].next)
    {
        int y=a[i].y;
        if(y==fa) continue;
        dfs(y,x,t);
        if(f[y]!=-1) f[x]=max(f[x],f[y]+1);
        if(g[y]!=-1) g[x]=max(g[x],g[y]-1);
    }
    if(f[x]<=g[x]) f[x]=-1;
    if(f[x]==t) f[x]=-1,g[x]=t,num++;
    if(x==1&&f[x]!=-1) num++;
}
bool check(int t)
{
    num=0;
    memset(f,-1,sizeof(f));
    memset(g,-1,sizeof(g));
    dfs(1,0,t);
    return num<=m;
}
int main()
{
    n=read();m=read();
    for(int i=1;i<=n;i++) d[i]=read();
    for(int i=1;i<n;i++)
    {
        int x,y;x=read();y=read();
        ins(x,y);ins(y,x);
    }
    int l=0,r=n,ans;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    printf("%d",ans);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值