Pat Jia 1004 counting leaves [DFS]

Source: Internet
Author: User
1004 counting leaves (30 points)

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]

WhereIDIs a two-digit number representing a given non-leaf node,KIs the number of its children, followed by a sequence of two-digitID'S of its children. For the sake of simplicity, let us fix the root ID to be01.

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, where01Is the root and02Is its only child. Hence on the Root01Level, there is0Leaf node; and on the next level, there is1Leaf node. Then we shoshould output0 1In a line.

Sample input:
2 101 1 02
Sample output:
0 1

 

Question:

Specify the relationship between a tree and a node. Count the number of leaf nodes on each layer.

Ideas:

Just build a new architecture, and it's good to have a violent DFS. When maxn = 105, WA and re are changed to 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 0x7f7f7f7f14 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("\n");69     return 0;70 }

 

Pat Jia 1004 counting leaves [DFS]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.