pat 1004

 

Counting Leaves 

 

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

水题一个,主要考察宽搜,树使用链接表的形式进行存储,方便搜索。

View Code
 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 #include<string.h>

 4 int A[101][101];

 5 int childNum[101];

 6 int levelNum;

 7 int N, M;

 8 int *currentList, *nextList;

 9 void init()

10 {

11     memset(A, 0, sizeof(int)*101*101);

12     memset(childNum, 0, sizeof(int)*101);

13     int i,j,ID,k;

14     i = 1;

15     while(i<=M){

16         scanf("%d %d", &ID, &k);

17         A[ID][0] = k; //record the child numchildNumer

18         for(j = 1;j<= k;j++)

19             scanf("%d", &A[ID][j]);

20         i++;

21     }

22 }

23 void countLevelChildNum(){

24 

25     int i,j,leafNodeNum,nextNodeNum,temp , *ptemp;

26     levelNum = 1;

27     if(A[1][0] ==0){

28         levelNum = 1;

29         childNum[1] =1;

30         return ;

31     }else{

32         levelNum++;

33         childNum[1] = 0;

34     }

35 

36     //init the current list

37     currentList[0] = A[1][0];

38     i =1;

39     while(i <= A[1][0]){

40         currentList[i] = A[1][i];

41         i++;

42     }

43     while(currentList[0])

44     {

45         leafNodeNum = 0;nextNodeNum =0;

46         for(i =1; i <= currentList[0] ;i++)

47         {    

48             temp = currentList[i];

49             if(A[temp][0] == 0)

50             {

51                 leafNodeNum++;continue; 

52             }

53             for(j =1; j<= A[temp][0];j++)

54             {

55                 nextNodeNum++;

56                 nextList[nextNodeNum] = A[temp][j];

57             }

58 

59         }

60         nextList[0] = nextNodeNum;

61         childNum[levelNum] = leafNodeNum;

62         levelNum ++;

63 

64         ptemp = currentList;

65         currentList = nextList;

66         nextList = currentList;

67     }

68     levelNum--;

69 

70 }

71 int main()

72 {

73     int i, flag = 0;

74 

75     currentList = (int *)malloc(sizeof(int) * 101);

76     if(currentList == NULL) return 0;

77     nextList = (int *)malloc(sizeof(int)*101);

78     if(nextList == NULL) return 0;

79 

80     while(scanf("%d %d",&N,&M) != EOF)

81     { 

82         init();

83         countLevelChildNum();

84     

85         if(flag == 1) 

86             printf("\n");

87         else 

88             flag = 1;

89 

90         printf("%d",childNum[1]);

91         for(i = 2; i <= levelNum; i++) 

92             printf(" %d", childNum[i]);

93     }

94 

95 

96     return 0;

97 }

 

你可能感兴趣的:(pat)