题目链接:https://vjudge.net/problem/UVA-1267

首先我们要把这样一棵无根树转换成有根树,那么树根我们可以直接使用$VOD$。

还有一个性质:如果深度为$d$的一个节点并不能被覆盖,那么我们在它的第$k$级的祖先(父亲为第一级)那里建一个$VOD$是最优的,其实很好证明它不仅能覆盖这些点,还能覆盖更多的点。

思路:

1.进行第一遍dfs,将无根树变成有根树。在建树的过程中用$node[d][i](d>k)$表示第$d$层有不能被覆盖到的点,那么可以避开“按深度排序”这个过程。然后对于$d\leq k$的情况我们直接当它不存在。

2.从$n-1$层倒着遍历,如果有点不能被覆盖到,那么就找它的$k$级祖先,在那里设上$VOD$,然后进行更新。

注意:

1.只需处理叶节点。    2.$u$和$father$之间是无向边!

AC代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<vector>
  4. #include<algorithm>
  5. #include<cstring>
  6. using namespace std;
  7.  
  8. const int maxn=;
  9. vector<int> g[maxn],node[maxn];
  10. int n,s,k,fa[maxn];
  11. bool vis[maxn];
  12.  
  13. void dfs(int u,int f,int d){
  14. fa[u]=f;
  15. int nc=g[u].size();
  16. if(nc==&&d>k) node[d].push_back(u);
  17. for(int i=;i<nc;i++){
  18. int v=g[u][i];
  19. if(v!=f) dfs(v,u,d+);
  20. }
  21. }
  22.  
  23. void dfs2(int u,int f,int d){
  24. vis[u]=;
  25. int nc=g[u].size();
  26. for(int i=;i<nc;i++){
  27. int v=g[u][i];
  28. if(v!=f&&d<k) dfs2(v,u,d+);
  29. }
  30. }
  31.  
  32. int solve(){
  33. int ans=;
  34. memset(vis,,sizeof(vis));
  35. for(int d=n-;d>k;d--){
  36. for(int i=;i<node[d].size();i++){
  37. int u=node[d][i];
  38. if(vis[u]) continue;
  39. int v=u;
  40. for(int j=;j<k;j++) v=fa[v];
  41. dfs2(v,-,);
  42. ans++;
  43. }
  44. }
  45. return ans;
  46. }
  47.  
  48. int main(){
  49. int T;
  50. scanf("%d",&T);
  51. while(T--){
  52. scanf("%d%d%d",&n,&s,&k);
  53. for(int i=;i<=n;i++){
  54. g[i].clear();
  55. node[i].clear();
  56. }
  57. for(int i=;i<n-;i++){
  58. int a,b;
  59. scanf("%d%d",&a,&b);
  60. g[a].push_back(b);
  61. g[b].push_back(a);
  62. }
  63. dfs(s,-,);
  64. printf("%d\n",solve());
  65. }
  66. return ;
  67. }

AC代码

UVA 1267 Network(DFS)的更多相关文章

  1. UVA - 11853 Paintball(dfs)

    UVA - 11853 思路:dfs,从最上面超过上边界的圆开始搜索,看能不能搜到最下面超过下边界的圆. 代码: #include<bits/stdc++.h> using namespa ...

  2. UVA - 1103Ancient Messages(dfs)

    UVA - 1103Ancient Messages In order to understand early civilizations, archaeologists often study te ...

  3. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  4. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  5. uva 725 Division(除法)暴力法!

    uva 725  Division(除法) A - 暴力求解 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  6. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  7. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  8. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  9. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

随机推荐

  1. Codeforces Round #624 (Div. 3)

    A.题意:通过加奇数减偶数的操作从a到b最少需要几步 签到题 #include <algorithm> #include <iostream> #include <cst ...

  2. 论文阅读笔记(十二)【CVPR2018】:Exploit the Unknown Gradually: One-Shot Video-Based Person Re-Identification by Stepwise Learning

    Introduction (1)Motivation: 大量标记数据成本过高,采用半监督的方式只标注一部分的行人,且采用单样本学习,每个行人只标注一个数据. (2)Method: 对没有标记的数据生成 ...

  3. 深入浅出Mybatis系列六-objectFactory、plugins、mappers简介与配置

    注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇文章<深入浅出Mybatis系列(五)---TypeHandler简介及配 ...

  4. Django生成脚本迁移文件时,报错django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

    一.本人环境:django:3.0.2, python:3.8.1,  pymysql:0.9.3 二.解决步骤: 1.django目录下找到 base.py文件: 2.在base.py文件中注释以下 ...

  5. jQuery---width和height的方法

    width和height的方法 //获取div的宽度 $("div").css("width", "400px"); console.log ...

  6. CodeForces 1144B

    原题https://vjudge.net/problem/CodeForces-1144B #include<bits/stdc++.h> using namespace std; vec ...

  7. LaTeX技巧010:LaTtex中如何给每个句子加序号?

    效果图: 代码: \documentclass{article} \newcounter{sentence} \renewcommand\thesentence{\textsuperscript{\a ...

  8. 从servlet向jsp中传数据用Java接收js调用

    servlet: response.sendRedirect("showMessage.jsp?ValueA=1"); jsp: var a=<%=request.getPa ...

  9. Apache服务:使用 Apache 服务部署静态网站

    1.安装Apache服务 第一步:安装Apache服务程序   yum install httpd 具体流程参考https://www.cnblogs.com/python-wen/p/1016845 ...

  10. print不是函数

    对文章阅读有感!!! 文章地址:http://www.laruence.com/2019/03/01/4904.html print是一个语法结构(language constructs), 他并不是 ...