zoukankan      html  css  js  c++  java
  • [HNOI 2003]消防局的设立

    Description

    2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地。起初为了节约材料,人类只修建了n-1条道路来
    连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状结构。如果基地A到
    基地B至少要经过d条道路的话,我们称基地A到基地B的距离为d。由于火星上非常干燥,经常引发火灾,人类决定
    在火星上修建若干个消防局。消防局只能修建在基地里,每个消防局有能力扑灭与它距离不超过2的基地的火灾。
    你的任务是计算至少要修建多少个消防局才能够确保火星上所有的基地在发生火灾时,消防队有能力及时扑灭火灾。

    Input

    第一行为n,表示火星上基地的数目。N<=1000
    接下来的n-1行每行有一个正整数,其中文件第i行的正整数为a[i],表示从编号为i的基地到编号为a[i]的基地之间有一条道路,
    为了更加简洁的描述树状结构的基地群,有a[i] < i

    Output

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

    Sample Input

    6
    1
    2
    3
    4
    5

    Sample Output

    2

    题解

    [UVa 1267]Network思路很类似,选最深的未盖点的$2$级祖先覆盖就好了。

     1 //It is made by Awson on 2017.11.3
     2 #include <set>
     3 #include <map>
     4 #include <ctime>
     5 #include <cmath>
     6 #include <queue>
     7 #include <stack>
     8 #include <vector>
     9 #include <cstdio>
    10 #include <string>
    11 #include <cstring>
    12 #include <cstdlib>
    13 #include <iostream>
    14 #include <algorithm>
    15 #define LL long long
    16 #define Max(a, b) ((a) > (b) ? (a) : (b))
    17 #define Min(a, b) ((a) < (b) ? (a) : (b))
    18 #define sqr(x) ((x)*(x))
    19 using namespace std;
    20 const int N = 1000;
    21 
    22 int n, a;
    23 struct tt {
    24     int to, next;
    25 }edge[(N<<1)+5];
    26 int path[N+5], top;
    27 int fa[N+5];
    28 struct ss {
    29     int u, a;
    30     bool operator < (const ss &b) const {
    31     return a > b.a;
    32     }
    33 }q[N+5];
    34 bool cover[N+5];
    35 
    36 void dfs(int u, int father, int dep) {
    37     q[u].u = u, q[u].a = dep, fa[u] = father;
    38     for (int i = path[u]; i; i = edge[i].next)
    39     if (edge[i].to != father) dfs(edge[i].to, u, dep+1);
    40 }
    41 void coverit(int u, int dep) {
    42     cover[u] = 1; if (dep == 0) return;
    43     for (int i = path[u]; i; i = edge[i].next)
    44     coverit(edge[i].to, dep-1);
    45 }
    46 void add(int u, int v) {
    47     edge[++top].to = v;
    48     edge[top].next = path[u];
    49     path[u] = top;
    50 }
    51 void work() {
    52     scanf("%d", &n);
    53     for (int i = 2; i <= n; i++) {
    54     scanf("%d", &a); add(a, i); add(i, a);
    55     }
    56     dfs(1, 0, 1);
    57     sort(q+1, q+1+n);
    58     int ans = 0;
    59     for (int i = 1; i <= n; i++) {
    60     if (cover[q[i].u]) continue;
    61     ans ++; int a = fa[fa[q[i].u]];
    62     if (!a) break;
    63     coverit(a, 2);
    64     }
    65     printf("%d
    ", ans);
    66 }
    67 int main() {
    68     work();
    69     return 0;
    70 }
  • 相关阅读:
    paper 89:视频图像去模糊常用处理方法
    paper 88:人脸检测和识别的Web服务API
    paper 87:行人检测资源(下)代码数据【转载,以后使用】
    paper 86:行人检测资源(上)综述文献【转载,以后使用】
    paper 85:机器统计学习方法——CART, Bagging, Random Forest, Boosting
    paper 84:机器学习算法--随机森林
    paper 83:前景检测算法_1(codebook和平均背景法)
    paper 82:边缘检测的各种微分算子比较(Sobel,Robert,Prewitt,Laplacian,Canny)
    paper 81:HDR成像技术
    paper 80 :目标检测的图像特征提取之(一)HOG特征
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7780580.html
Copyright © 2011-2022 走看看