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);
}

贪心【CF1029E】Tree with Small Distances的更多相关文章

  1. CF1029E Tree with Small Distances (贪心)

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

  2. CF1029E Tree with Small Distances

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

  3. 树的最小支配集 E - Cell Phone Network POJ - 3659 E. Tree with Small Distances

    E - Cell Phone Network POJ - 3659 题目大意: 给你一棵树,放置灯塔,每一个节点可以覆盖的范围是这个节点的所有子节点和他的父亲节点,问要使得所有的节点被覆盖的最少灯塔数 ...

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

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

  5. CF E .Tree with Small Distances(树上的贪心)

    题意: 这是一颗有n-1条边的无向树 , 在树上加最少的边使树的1节点到其他节点的距离最多为 2 : 分析:很容易考虑的贪心的做法,但是该如何的贪心呢 ? 我一开始是打算贪心节点的儿子最多那一个 , ...

  6. codeforces 1029E Tree with Small Distances【思维+贪心】 【非原创】

    题目:戳这里 学习博客:戳这里 题意:给一个树加最少的边,使得1到所有点的距离小于等于2. 解题思路:分析样例3可以看出,如果一个点到1的距离大于2,那么建立1到该点的父亲节点的边将比直接与该点建边更 ...

  7. CF 1029E Tree with Small Distances

    昨晚随便玩玩搞个div3结果浪翻了…… 强烈谴责D题hack数据卡常 考虑到本题中所要求的最短距离不会大于2,所以我们可以把所有结点到$1$的距离通过对$3$取模分类,考虑到直接自顶向下贪心不满足局部 ...

  8. Codeforces 1029 E. Tree with Small Distances(树上dp)

    题目直通车:http://codeforces.com/problemset/problem/1029/E 思路大意:在树上做dp,依次更新ar数组,ar[i]表示以i为根节点的子树对答案的最小贡献值 ...

  9. Codeforces Round #506 (Div. 3) D-F

    Codeforces Round #506 (Div. 3) (中等难度) 自己的做题速度大概只尝试了D题,不过TLE D. Concatenated Multiples 题意 数组a[],长度n,给 ...

随机推荐

  1. 【51NOD】消灭兔子

    [算法]贪心 #include<cstdio> #include<algorithm> #include<cstring> #include<queue> ...

  2. Spring 事务管理(山东数漫江湖)

    最新又重新学习了一遍Spring的事务,这里做点总结,不做如何一步步配置的流水账. 1. 关键类 public interface PlatformTransactionManager { Trans ...

  3. Windows Server 2008 R2 SP1安装SQL 2012安装报错之0x858C001B

    使用Windows Server 2008 R2 SP1安装SQL 2012的时候总是报这样一个错: SQL Server Setup has encountered the following er ...

  4. LESS使用简介!

    我真的真的极度痛苦. 原本用了那么久的LESS,一直都是用编译工具(考拉)进行编译的,今天试了试用less.js来搞,按官网的都一毛一样,然而!就是编译不出来! 我用来擦鼻涕的卫生纸都一下午用了大半卷 ...

  5. 一种通过HTTP传文件出网的姿势

    在外网机器上运行文件服务接收服务 root@kali:~/pentest-script/FileTransfer/HttpServer# python3 SimpleHttpUpload.py Ser ...

  6. 利用certutil.exe 传文件

    certutill.exe 在Windows 7 及其之后的所有Windows Server和Workstation版本均预装 1. Encode file: certutil -encode kk. ...

  7. android隐藏EditText光标

    在android中如果有EditText,那么在载入时,光标会默认显示在第一个EditText框中,如果不想显示光标,且也不想把该光标移动到下一个EditText框,最简单的方法是在该 EditTex ...

  8. linux中断线程化分析【转】

    转自:http://blog.csdn.net/qq405180763/article/details/24120895 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在为3.8版本的Li ...

  9. python爬虫模块之HTML下载模块

    HTML下载模块 该模块主要是根据提供的url进行下载对应url的网页内容.使用模块requets-HTML,加入重试逻辑以及设定最大重试次数,同时限制访问时间,防止长时间未响应造成程序假死现象. 根 ...

  10. caffe solver.prototxt 生成

    from caffe.proto import caffe_pb2 s = caffe_pb2.SolverParameter() path='/home/xxx/data/' solver_file ...