TopCoder 13955《WalkOverATree》
Problem Statement
Given is a tree on n nodes. The nodes are numbered 0 through n-1. You are given the description of the tree as a int[] parent with n-1 elements. For each valid i, there is an edge between vertices (i+1) and parent[i].
A person is currently standing in node 0. In a single step, the person can move from its current node to any adjacent node. You are given an int L. The person is allowed to make at most L steps.
Return the maximum number of nodes the person can visit during the walk. Node 0 (where the walk starts) and the node where the walk ends count as visited. Each visited node is only counted once, even if it is visited multiple times.
Definition
Class: WalkOverATree
Method: maxNodesVisited
Parameters: int[], int
Returns: int
Method signature: int maxNodesVisited(int[] parent, int L)
(be sure your method is public)
Constraints
parent will contain between 0 and 49 elements, inclusive.
For each i, parent[i] will be between 0 and i, inclusive.
L will be between 1 and 100, inclusive.
Examples
请自行到 vjudge 上寻找
解析
英文很好懂,只需人教初二水平(反正我准初三选手看懂了)
题目大意:
给定一棵 n 个点的树,编号 0~n-1。连边方式以输入每个点的父亲给出,对于每个 i,有一条边连接点 (i+1) 和点 father[i],而且 father[i] 是 (i+1) 的父亲。
有一个人站在点 0,可以向四周走不超过 L 步,求出这个人能经过多少不同的点
这题和 dp 有什么关系吗。。。
这题的难点大概就是 class 的使用和答案统计了吧
class 的使用可以参考这里
答案统计和基本的思路见代码吧
代码实现
1 |
|
- 本文作者:Handwer STD
- 本文链接:https://blog.handwer-std.top/2019-07-31/TopCoder13955/index.html
- 版权声明:本博客所有文章均采用 BY-NC-SA 许可协议,转载请注明出处!