最小覆盖问题

题目描述

2020 年,人类在火星上建立了一个庞大的基地群,总共有 nn 个基地。起初为了节约材料,人类只修建了 n-1n−1 条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状结构。如果基地 AA 到基地 BB 至少要经过 dd 条道路的话,我们称基地A到基地B的距离为 dd。

由于火星上非常干燥,经常引发火灾,人类决定在火星上修建若干个消防局。消防局只能修建在基地里,每个消防局有能力扑灭与它距离不超过 22 的基地的火灾。

你的任务是计算至少要修建多少个消防局才能够确保火星上所有的基地在发生火灾时,消防队有能力及时扑灭火灾。

输入格式

输入文件的第一行为 nn(1 \leq n \leq 10001≤n≤1000),表示火星上基地的数目。接下来的 n-1n−1 行每行有一个正整数,其中文件第 ii 行的正整数为 a_iai​,表示从编号为 ii 的基地到编号为 a_iai​ 的基地之间有一条道路,为了更加简洁的描述树状结构的基地群,有 a_i\lt iai​<i。

输出格式

仅有一个正整数,表示至少要设立多少个消防局才有能力及时扑灭任何基地发生的火灾。

树形dp解法

#include<iostream>
#include<cstring>
using namespace std;
const int maxn=1e3+5;
const int INF=1e9;
int head[maxn];
struct node{
    int to,next;
}edge[maxn<<1];
int cnt=0;
void add_edge(int from,int to)
{
    edge[cnt].to=to;
    edge[cnt].next=head[from];
    head[from]=cnt++;
}
int f[maxn][5];
void dfs(int root,int pre)
{
    int sum2=0,sum3=0,tot=0;
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(to!=pre)
        {
            dfs(to,root);
            sum2+=f[to][2];
            sum3+=f[to][3];
            tot++;
        }
    }
    if(!tot)
    {
        f[root][0]=1;f[root][1]=1;f[root][2]=1;
        return;
    }
    f[root][0]=1;f[root][1]=INF;f[root][2]=INF;
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(to!=pre)
        {
            f[root][0]+=f[to][4];
            f[root][1]=min(f[root][1],f[to][0]+sum3-f[to][3]);
            f[root][2]=min(f[root][2],f[to][1]+sum2-f[to][2]);
            f[root][3]+=f[to][2];
            f[root][4]+=f[to][3];
        }
    }
    for(int i=1;i<5;i++)
    {
        f[root][i]=min(f[root][i],f[root][i-1]);
    }
}
int main()
{
    int n;cin>>n;
    memset(head,-1,sizeof(head));
    for(int i=1;i<n;i++)
    {
        int now;cin>>now;
        add_edge(i+1,now);
        add_edge(now,i+1);
    }
    dfs(1,0);
    cout<<f[1][2]<<endl;
}

贪心:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e3+5;
const int INF=1e9;
int dep[maxn];
int fa[maxn];
int dis[maxn];
int head[maxn],id[maxn];
struct node{
    int to,next;
}edge[maxn<<1];
int cnt=0;
bool cmp(int a,int b)
{
    return dep[a]>dep[b];
}
void add_edge(int from,int to)
{
    edge[cnt].to=to;
    edge[cnt].next=head[from];
    head[from]=cnt++;
}
void dfs(int root,int pre)
{
    dep[root]=dep[pre]+1;
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(to!=pre)
        {
            fa[to]=root;
            dfs(to,root);
        }
    }
}
int main()
{
    int n;cin>>n;
    memset(head,-1,sizeof(head));
    for(int i=1;i<n;i++)
    {
        int from;cin>>from;
        add_edge(from,i+1);
        add_edge(i+1,from);
        id[i]=i;
    }
    id[n]=n;
    dfs(1,0);
    sort(id+1,id+1+n,cmp);
    memset(dis,0x3f,sizeof(dis));
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        int w=id[i];
        dis[w]=min(dis[w],dis[fa[w]]+1);
        dis[w]=min(dis[w],dis[fa[fa[w]]]+2);
        if(dis[w]>2)
        {
            ans++;
            int grf=fa[fa[w]];
            dis[grf]=0;
            dis[fa[grf]]=min(dis[fa[grf]],1);
            dis[fa[fa[grf]]]=min(dis[fa[fa[grf]]],2);
        }
    }
    cout<<ans<<endl;
}

题目描述

The Byteotian Cave is composed of nn chambers and n-1n−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 mm 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.

输入格式

The first line of the standard input holds two integers nn and mm(1\le m\le n\le 300\ 0001≤m≤n≤300 000), 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 nn.

The next line contains nn integers d_1,d_2,\cdots,d_nd1​,d2​,⋯,dn​ (d_i\in \{0,1\}di​∈{0,1}), separated by single spaces.

If d_i=1di​=1, then there is dynamite in the ii-th chamber, and if d_i=0di​=0, there is none.The following n-1n−1 lines specify the corridors of the cave. Each of them holds two integers a,ba,b(1\le a<b\le n1≤a<b≤n), separated by a single space, denoting that there is a corridor connecting the chambers aa and bb. Every corridor appears exactly once in the description.

You may assume that in tests worth 10% of the points it holds additionally that n\le 10n≤10 , while in tests worth 40% of the points it holds that n\le 1\ 000n≤1 000

输出格式

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.

题意翻译

给一棵树,树上有一些关键节点,要求你选 mm 个点,第 ii 个关键节点到这些点中每个点距离的最小值记为 dis_idisi​,记这全部 disdis 的最大值为 KK,现在要使 KK 最小,求这个 KK。

输入输出样例

输入 #1复制

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

输出 #1复制

1
#include<iostream>
#include<cstring>
using namespace std;
int n,m;
const int maxn=3e5+5;
const int INF=1e9;
int head[maxn],val[maxn];
struct node{
    int to,next;
}edge[maxn<<1];
int cnt;
void add_edge(int from,int to)
{
    edge[cnt].to=to;
    edge[cnt].next=head[from];
    head[from]=cnt++;
}
int f[maxn],g[maxn];
int tot;
void dfs(int root,int pre,int x)
{
    f[root]=-INF,g[root]=INF;
    for(int i=head[root];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(to==pre) continue;
        dfs(to,root,x);
        f[root]=max(f[root],f[to]+1);
        g[root]=min(g[root],g[to]+1);
    }
    if(g[root]+f[root]<=x) f[root]=-INF;
    if(g[root]>x&&val[root]==1) f[root]=max(f[root],0);
    if(f[root]==x) f[root]=-INF,g[root]=0,tot++;
}
int main()
{
    memset(head,-1,sizeof(head));
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>val[i];
    for(int i=1;i<n;i++)
    {
        int from,to;cin>>from>>to;
        add_edge(from,to);
        add_edge(to,from);
    }
    int l=0,r=maxn;
    int ans;
    while(l<=r)
    {
        int mid=(l+r)/2;
        tot=0;
        dfs(1,0,mid);
        if(f[1]>=0) tot++;
        //cout<<mid<<" "<<tot<<endl;
        if(tot<=m)
        {
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    cout<<ans<<endl;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值