一个看起来就不对的贪心居然是正解...

但仔细思考一下,这种贪心倒的确找不到反例..

贪心思想:每次找出离根节点最远的点,然后由根节点向这个点的父节点连边,一直连到所有点都能被覆盖即可,这样构造出的一定是一个可行的最优解

正确性证明(个人YY):

主要是要证明这种做法的最优性:

首先,由于所有点都要求被覆盖,自然离根节点最远的点也不例外

那么,如果想覆盖上离根节点最远的点,只会有两种覆盖方法:一种是将根节点与这个点本身相连,另一种是将根节点与这个点的父节点相连

不难发现,将根节点与这个点的父节点相连,这样的方案一定不会差

证明:假设这个父节点还有别的子节点,那么与父节点相连后这些子节点都能被覆盖,这样一定是更优的

而即使这个父节点没有别的子节点,他还有自己的父节点,这样连边也能减少根节点与他的父节点的距离,也会达到更好的效果

即使上面两点都没有起作用,至少这样还可以覆盖上最远的点,也并不会使代价增大,所以这样做是完全可行的。

这样就完事了

(ps:树上bfs真好用)

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
struct Edge
{
int next;
int to;
}edge[400005];
bool used[200005];
int head[200005];
int f[200005];
int n;
int cnt=1;
void init()
{
memset(head,-1,sizeof(head));
cnt=1;
}
void add(int l,int r)
{
edge[cnt].next=head[l];
edge[cnt].to=r;
head[l]=cnt++;
}
struct node
{
int num;
int dep;
}p[200005];
void bfs(int rt)
{
queue <int> M;
p[rt].dep=0;
p[rt].num=rt;
M.push(rt);
while(!M.empty())
{
int u=M.front();
M.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(p[to].dep)
{
continue;
}
p[to].dep=p[u].dep+1;
p[to].num=to;
f[to]=u;
M.push(to);
if(p[to].dep<=2)
{
used[to]=1;
}
}
}
}
bool cmp(node a,node b)
{
return a.dep>b.dep;
}
int main()
{
scanf("%d",&n);
init();
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
bfs(1);
sort(p+1,p+n+1,cmp);
int ans=0;
for(int i=1;i<=n;i++)
{
if(used[p[i].num])
{
continue;
}
ans++;
int u=f[p[i].num];
used[u]=1;
for(int j=head[u];j!=-1;j=edge[j].next)
{
int to=edge[j].to;
used[to]=1;
}
}
printf("%d\n",ans);
return 0;
}

CF1029E的更多相关文章

  1. Tree with Small Distances(cf1029E)(树形动规)

    You are given an undirected tree consisting of \(n\) vertices. An undirected tree is a connected und ...

  2. 贪心【CF1029E】Tree with Small Distances

    Description 给定一棵树.要求往树中加入一些边使得从1到其他节点的距离至多是2 . 输出加入边的最小数量.(边全部都是无向的) Input 第一行一个整数n,表示树中的节点个数. 接下来n− ...

  3. CF1029E Tree with Small Distances

    题目描述 给定一棵树.要求往树中加入一些边使得从1到其他节点的距离至多是2 . 输出加入边的最小数量.(边全部都是无向的) 题解:好多人都说是贪心,但是我写的是树形dp. (这道题实在太像小胖守皇宫了 ...

  4. CF1029E Tree with Small Distances (贪心)

    题目大意:给你一棵边权为1的树,让你加入一些边,使得根节点(1号节点)到其他节点的最短距离不大于2 并没有想到贪心...... 正解的贪心思路是这样的 用一个堆维护当前距离最远的点,然后把根节点和它的 ...

  5. 树形dp——cf1029E

    题解给出的是带log的,,我自己写了个on的.. #include<bits/stdc++.h> #include<vector> using namespace std; # ...

随机推荐

  1. Django学习手册 - reverse()反转URL

    前端: <h1>测试</h1> <a href="/ce_test/?id=1">1按键</a> <a href=" ...

  2. Django学习手册 - ORM 外键

    Django 外键创建 关键语法: models.ForeignKey("UserGroup",to_field="gid",default=1,on_dele ...

  3. okhttp3与旧版本okhttp的区别分析

    https://www.jianshu.com/p/4a8c94b239b4  待总结学习

  4. 1421 - Wavio Sequence

    题目大意:求一个序列中 先严格递增后严格递减的子序列的数目(要求这个子序列对称). 题目思路:正一遍DP,反一遍DP,因为n<=1e5,dp要把时间压缩到nlogn #include<st ...

  5. oracle 清理跟踪文件trc,trm

    oracle的日志文件有以下几种: 警告日志,trace日志,audit日志,redo日志,归档日志 a.警告日志:也就是alert log,使用初始化参数 show parameter backgr ...

  6. java ArrayList、Vector、LinkedList区别

  7. 20165221 2017-2018-2《Java程序设计》课程总结

    20165221 2017-2018-2<Java程序设计>课程总结 一.作业连接汇总 每周作业链接 预备作业一: 20165221我期望的师生关系 预备作业二: 20165221学习基础 ...

  8. WPF DataGrid 列显示0,-1(作废、删除)状态,1,2(支出、收入)类型,操作人(在其他表中),如何转换格式。

    操作人,左联,Join on letf //容我补充 状态,类型,类似的转换,在xmlns中引入common   xmlns:com="clr-namespace:XXX.Common&qu ...

  9. linux shell 之尝试编写 企业级 启动脚本

    企业Shell面试题10:开发企业级MySQL启动脚本 说明: MySQL启动命令为: 1 /bin/sh mysqld_safe --pid-file=$mysqld_pid_file_path 2 ...

  10. SQL Server Profiler 常见问题总结

    1.跟踪指定数据库 SELECT DB_ID('数据名称') 原文:https://jingyan.baidu.com/article/647f0115be128a7f2048a87d.html 2. ...