PAT Grade A 1004 Two-Dimensional Array Implementation

topic

1004. Counting Leaves (30)

time limit
400 ms
memory limit
65536 kB
code length limit
16000 B
Judgment procedure
Standard
author
CHEN, Yue
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

 

Input

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.

Output

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 


The general idea of ​​the title is to give a tree, find the number of leaf nodes in each layer of the tree,
n is the total number of nodes in the tree, m is the number of non-leaf nodes, and the input of each line of the next m lines is ID (the node point) K (the number of children of the node) ID[1] (child 1) ID[2] (child 2) ... ID[K] (child k)

The meaning of the question is obviously to be written in a tree, but I do the question When I suddenly had a bold idea, I came up with the following code
#include<iostream> 
#include <cstdio> 
#include <cstring>
 using  namespace std;
 int tree[ 105 ][ 105 ];
 int no[ 105 ]; // Number of leaves per layer 
int n,m;
 int ma; / / Maximum number of layers 
void dfs( int s)
{
    for(int i=1;i<=n;i++)
    {
        if(tree[s][i])
        {
            if(tree[i][0]!=(tree[s][0]+1))
            {
                tree[i][0]=tree[s][0]+1;
                if(tree[i][0]>ma)ma=tree[i][0];
            }
            dfs(i);
        }
    }
}
intmain ()
{
    int a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(no,0,sizeof(no));
        memset(tree,0,sizeof(tree));
        ma = 1 ;
        tree[ 1 ][ 0 ]= 1 ; // The first element stores the number of layers of the node 
        for ( int i= 0 ;i<m;i++ )
        {
            scanf("%d%d",&a,&b);
            tree[a][104]=b;
            for(int j=0;j<b;j++)
            {
                scanf("%d",&c);
                tree[a][c]=1;
            }
        }
        dfs(1);
        for(int i=1;i<=n;i++)
        {

            if(tree[i][104]==0)
            {
                no[tree[i][0]]++;
            }
        }
        for(int i=1;i<=ma;i++)
        {
            printf("%d",no[i]);
            if(i!=ma)printf(" ");
        }
        printf("\n");
    }
    return 0;
}

The general idea is to use a (n+2)*(n+2) two-dimensional array. The first bit of each row stores the number of layers of the node, and the last bit stores the number of children. If j is the child of node i, then Set tree[i][j] to 1.

After entering the entire tree, start from the root node to search for the number of layers of each node marked, and after completion, traverse the last element of each line to record the leaf nodes.

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325163602&siteId=291194637