PAT甲1004 Counting Leavesdfs

Posted wyboooo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT甲1004 Counting Leavesdfs相关的知识,希望对你有一定的参考价值。

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 }

 

以上是关于PAT甲1004 Counting Leavesdfs的主要内容,如果未能解决你的问题,请参考以下文章

PAT1004 Counting Leaves

pat 1004 Counting Leaves

PAT 甲级 1004 Counting Leaves

PAT Advanced 1004 Counting Leaves

PAT 1004 Counting Leaves(结构体)

PAT 1004 Counting Leaves (30分)