贪心【CF1029E】Tree with Small Distances

Description

给定一棵树。要求往树中加入一些边使得从1到其他节点的距离至多是2 。 输出加入边的最小数量。(边全部都是无向的)

Input

第一行一个整数n,表示树中的节点个数。 接下来n−1行,每行两个整数x,y,表示x,y之间有一条连边。

Output

输出一个整数,表示加入边的最小数量。

贪心做法,

我们每次选择深度最深的点,向其父节点加边来标记其他点。(到达这些点的距离都不超过\(2\).)

为什么向其父亲节点加边?因为这样会覆盖比较多的点。

如果遇到被标记的点,就\(continue\).

不太会证明正确性,但是可行。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#define R register

using namespace std;

const int gz=2e5+8;

inline void in(R int &x)
{
    R int f=1;x=0;char s=getchar();
    while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
    while(isdigit(s)){x=x*10+s-'0';s=getchar();}
    x*=f;
}

int head[gz],tot,depth[gz],ans,n,f[gz],cnt;

bool ok[gz];

struct cod{int u,v;}edge[gz<<1];

inline void add(R int x,R int y)
{
    edge[++tot].u=head[x];
    edge[tot].v=y;
    head[x]=tot;
}

struct hop
{
    int u,dep;
    bool operator <(const hop&a)const
    {
        return dep>a.dep;
    }
}q[gz];


void dfs(R int u,R int fa)
{
    depth[u]=depth[fa]+1;f[u]=fa;
    if(depth[u]>2)q[++cnt].u=u,q[cnt].dep=depth[u];
    for(R int i=head[u];i;i=edge[i].u)
    {
        if(edge[i].v==fa)continue;
        dfs(edge[i].v,u);
    }
}

int main()
{
    in(n);
    for(R int i=1,x,y;i<n;i++)
    {
        in(x),in(y);
        add(x,y),add(y,x);
    }
    depth[0]=-1;
    dfs(1,0);
    sort(q+1,q+cnt+1);
    for(R int j=1;j<=cnt;j++)
    {
        R int u=q[j].u;
        if(ok[u])continue;
        u=f[u];
        ok[u]=true;
        for(R int i=head[u];i;i=edge[i].u)
            ok[edge[i].v]=true;
        ans++;
    }
    printf("%d",ans);
}

转载于:https://www.cnblogs.com/-guz/p/9927049.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值