Algorithm Topcoder SRM666第1部分树分析

Algorithm Topcoder SRM666第1部分树分析,algorithm,tree,Algorithm,Tree,我的问题是针对以下问题陈述的特定输入模式 Say if input is {0,0,0,0,2,4,3,1} 4 输出应该是3,对吗?为什么大多数输出都是4? 这棵树看起来像 0->1->8,0->2->5,0->3->7,0->4->6 最大高度为2(因此为2步),因此回溯必须到根节点(第0个节点),即2步。所以他只能访问3个节点?但答案似乎不正确,是4。我错过了什么 问题陈述如下-来自TopCoder SRM666 Div1 给定的是n个节

我的问题是针对以下问题陈述的特定输入模式

Say if input is
{0,0,0,0,2,4,3,1}
4
输出应该是3,对吗?为什么大多数输出都是4? 这棵树看起来像

0->1->8,0->2->5,0->3->7,0->4->6
最大高度为2(因此为2步),因此回溯必须到根节点(第0个节点),即2步。所以他只能访问3个节点?但答案似乎不正确,是4。我错过了什么

问题陈述如下-来自TopCoder SRM666 Div1

给定的是n个节点上的一棵树。节点编号为0到n-1。您可以将树描述为具有n-1个元素的int[]父级。对于每个有效i,顶点(i+1)和父[i]之间都有一条边

有人当前站在节点0中。在单个步骤中,用户可以从其当前节点移动到任何相邻节点。给你一个整数L。这个人最多可以走L步

返回人员在漫游期间可以访问的最大节点数。节点0(漫游开始处)和漫游结束处的节点视为已访问。每个被访问的节点只统计一次,即使它被访问了多次

定义

Class:  WalkOverATree
Method: maxNodesVisited
Parameters: int[], int
Returns:    int
Method signature:   int maxNodesVisited(int[] parent, int L)
(be sure your method is public)

Examples 
0)  
         {0, 0} 2 Returns: 2 The tree consists of edges 1-0 and 2-0. Our person will start in node 0 and can make at most L=2 steps. In two
 steps, the best we can do is visit one of the nodes 1 and 2. 

1)  
         {0, 0} 3 Returns: 3 This is the same tree, only now we have L=3. In three steps the person can visit all three nodes: for example, by
 going from node 0 to node 1, back to node 0, and finally to node 2.
 Note that even though the person visited node 0 twice, we only count
 it once. 

2)  
         {0, 1, 2, 3} 2 Returns: 3

有了4个步骤,用户可以进行0->1->0->2->5,访问的节点总数等于4(即0、1、2和5)。啊!你是对的。现在有道理了。谢谢