zoukankan      html  css  js  c++  java
  • PAT甲1004 Counting Leaves【dfs】

    1004 Counting Leaves (30 分)

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

    Input Specification:

    Each input file contains one test case. Each case starts with a line containing 0<N<100, the number of nodes in a tree, and M (<N), the number of non-leaf nodes. Then M lines follow, each in the format:

    ID K ID[1] ID[2] ... ID[K]
    

    where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.

    The input ends with N being 0. That case must NOT be processed.

    Output Specification:

    For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

    The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.

    Sample Input:

    2 1
    01 1 02
    

    Sample Output:

    0 1

    题意:

    给定一棵树和节点之间的关系。要求统计每一层的叶子节点个数。

    思路:

    就建树,暴力dfs就好了。maxn=105的时候WA和RE了,改成了1005就过了。

     1 #include <iostream>
     2 #include <set>
     3 #include <cmath>
     4 #include <stdio.h>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <queue>
     9 #include <map>
    10 #include <bits/stdc++.h>
    11 using namespace std;
    12 typedef long long LL;
    13 #define inf 0x7f7f7f7f
    14 
    15 const int maxn = 1005;
    16 int n, m;
    17 struct node{
    18     int v, nxt;
    19 }edge[maxn];
    20 int head[maxn], tot = 0;
    21 int cnt[maxn], dep = -1;
    22 
    23 void addedge(int u, int v)
    24 {
    25     edge[tot].v = v;
    26     edge[tot].nxt = head[u];
    27     head[u] = tot++;
    28     edge[tot].v = u;
    29     edge[tot].nxt = head[v];
    30     head[v] = tot++;
    31 }
    32 
    33 void dfs(int rt, int fa, int h)
    34 {
    35     int sum = 0;
    36     dep = max(dep, h);
    37     for(int i = head[rt]; i != -1; i = edge[i].nxt){
    38         if(edge[i].v == fa)continue;
    39         sum++;
    40         dfs(edge[i].v, rt, h + 1);
    41     }
    42     if(sum == 0){
    43         cnt[h]++;
    44     }
    45 
    46 }
    47 
    48 int main()
    49 {
    50     scanf("%d%d", &n, &m);
    51     memset(head, -1, sizeof(head));
    52     for(int i = 0; i < m; i++){
    53         int u, k;
    54         scanf("%d %d", &u, &k);
    55         for(int j = 0; j < k; j++){
    56             int v;
    57             scanf("%d", &v);
    58             addedge(u, v);
    59         }
    60     }
    61 
    62     dfs(1, -1, 1);
    63     //cout<<dep<<endl;
    64     printf("%d", cnt[1]);
    65     for(int i = 2; i <= dep; i++){
    66         printf(" %d", cnt[i]);
    67     }
    68     printf("
    ");
    69     return 0;
    70 }
  • 相关阅读:
    防火墙透明模式
    HP管理工具System Management Homepage安装配置
    kbmmw 中JSON 中使用SQL 查询
    kbmmw 中JSON 操作入门
    第一个kbmmw for Linux 服务器
    kbmmw 5.02发布
    kbmmw 5.01 发布
    使用delphi 10.2 开发linux 上的Daemon
    使用unidac 在linux 上无驱动直接访问MS SQL SERVER
    使用delphi 10.2 开发linux 上的webservice
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9886333.html
Copyright © 2011-2022 走看看