Home > Back-end >  PAT A 1004 CountingLeaves A problem don't quite understand
PAT A 1004 CountingLeaves A problem don't quite understand

Time:12-04

The question is: code inside the recursive DFS function parameters don't know why I + + the depth is not AC (only 6 points), the depth + 1 to just go
Don't know what place is better problem
//DFS (a [n]. The at (I), + + the depth); ,
//DFS (a [n]. The at (I), the depth + 1);
//here can't use DFS (a [n]. The at (I), + + the depth); And don't know why


This is a topic
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 & lt; N
ID ID K [1] [2] ID... 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.

The 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 two nodes, where is The root 01 and 02 is its only child. Hence on The root 01 level, there is zero The leaf node; And on the next level, there is a leaf node. Then we should output 0 1 in a line.

The Sample Input:
2 1
01, 02 1
The Sample Output:
0 1




My code:
#include
using namespace std;
#include
#include
# define Max 105
Vector A, (Max).//vector to use, define a 2 d array variable length
Int N, M;
Int MaxDep=1;
Int the leaf (Max)={0};
Void DFS (int n, int the depth)
{
If (a [n]. The size ()==0)
{
The leaf [the depth] + +;
MaxDep=Max (MaxDep, the depth);
return ;
}
The else
{
For (int I=0; i{
//DFS (a [n]. The at (I), + + the depth);
DFS (a [n]. The at (I), the depth + 1);//here can't use DFS (a [n]. The at (I), + + the depth); And don't know why
}
}
return ;
}
Int main ()
{
# ifdef LOCAL
Freopen (" C: \ \ Users \ \ Lao tze invincible world a \ \ Desktop \ \ 1002. TXT ", "r", stdin);
# endif
The scanf (" % d % d ", & amp; N, & amp; M);
Int r, k, c;
for(int i=1; i<=M; I++)
{the scanf (" % d % d ", & amp; R, & amp; K);
For (int j=1; j<=k; J++)
{
The scanf (" % d ", & amp; C);
A [r]. Push_back (c);//add a [r] rear
}
}
DFS (1, 1);
Printf (" % d ", the leaf [1]).
for(int i=2; i<=MaxDep; I++)
Printf (" % d ", the leaf [I]);
return 0;
}

CodePudding user response:

For (int I=0; i
{
//DFS (a [n]. The at (I), + + the depth);
(1)
DFS (a [n]. The at (I), the depth + 1);//here can't use DFS (a [n]. The at (I), + + the depth); And don't know why (2)
(3)
}
Look from your description, it should be:
If it is (2) + + the depth after the recursive call is in (3), the depth is the depth + 1;
And (2) was introduced into the depth + 1, after the recursive, (3) is still a (1) in the depth
  • Related