• 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 }
  • 相关阅读:
    虚方法表与动态分派机制
    方法重载与invokevirtual字节码指令的关系
    栈桢与操作数栈以及符号引用与直接引用的转换
    通过字节码分析this关键字以及异常表的作用
    JVM synchronized关键字所生成的字节码
    window Chrome 下允许跨域访问服务端接口设置
    JVM Java字节码方法表与属性
    JVM 字节码的结构
    Jar hell问题以及解放方法
    JVM 线程上下文类加载器
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9886333.html
Copyright © 2020-2023  润新知