[英]Find minimum value needed to make all tree nodes zero by incrementing asjacent nodes at each step
The problem is about finding the minimum value needed to make all nodes zero, let's call it K. 问题在于找到使所有节点为零所需的最小值,我们称其为K。
A non-binary tree of numbers is given. 给出了一个非二进制数字树。
In the first step you can choose one of the nodes to start at. 在第一步中,您可以选择一个节点作为起点。 If K is greater than that node's value, you change that value to zero, and increment the value of the other nodes which are at a distance of one or two. 如果K大于该节点的值,则将该值更改为零,并增加相距一到两个距离的其他节点的值。 Note that once a node's value becomes zero, it wont be incremented any more and it wont allow nodes which are connected to it be incremented. 请注意,一旦节点的值变为零,就不会再增加它,也不会允许与其连接的节点增加。
Then you should choose another node which has at least one zero-valued node at distance one, and repeat the process 然后,您应该选择另一个在距离1处具有至少一个零值节点的节点,然后重复该过程
5
\
2
\
5
when we start from the leaf with value 5, we have 当我们从值5的叶子开始时,我们有
6
\
3
\
0
Then we should choose 3; 然后我们应该选择3; we can't choose 6 because it has no zero node in distance one! 我们不能选择6,因为它在距离1中没有零节点!
7
\
0
\
0
At the end we choose 7 and K = 7 but if we choose 2 at first then we have: 最后,我们选择7且K = 7,但是如果首先选择2,则我们有:
6
\
0
\
6
Then we should choose 6; 那么我们应该选择6; because the node with value two is zero now, the connection is cut and by changing value of node with value six, no more increment would happen! 因为现在值为2的节点为零,所以连接被切断,并且通过更改值为6的节点的值,将不再发生增量!
0
\
0
\
6
So minimum K = 6 所以最小K = 6
Find the max node and start from it (if there is more than one max node, the one which comes sooner is chosen) 找到最大节点并从中开始(如果有多个最大节点,则选择较早的那个)
I define an array, let's call it possible nodes , and I add the node found in step 1 to it. 我定义了一个数组,我们将其称为可能的节点 ,然后向其中添加在步骤1中找到的节点。
While possible nodes is not empty, I perform the following steps: 当可能的节点不为空时,我执行以下步骤:
a. 一种。 Choose the max value in possible nodes; 在可能的节点中选择最大值; let's call it max_node 我们称之为max_node
b. 湾 Make max_node power zero and update K 使max_node功率为零并更新K
c. C。 Increment the value of its parent, grand parent, children, grand children and siblings (if they were not zero before) 增加其父母,祖父母,子女,孙子女和兄弟姐妹的价值(如果之前不为零)
d. d。 Add its parent and children with non-zero values to possible nodes 将其非零值的父项和子项添加到可能的节点
e. 即 Remove max_node from possible nodes 从可能的节点中删除max_node
Actually this is a homework problem, but this approach is not the right one! 实际上这是一个作业问题,但是这种方法不是正确的方法! It gives wrong answers and hits time-out limits. 它给出错误的答案并达到超时限制。
Number of nodes ≤ 3×10 5 节点数≤3×10 5
-10 9 ≤ value of nodes ≤ 10 9 节点-10 9≤值≤10 9
Time limit: 2.5 seconds 时间限制:2.5秒
Memory limit: 256 MB 内存限制:256 MB
If you start with a random node in the tree, then K is the maximum of: 如果从树中的随机节点开始,则K为以下各项的最大值:
This is because of the restriction that you can only choose nodes with 0-nodes adjacent to it and that those are not incremented. 这是因为限制,您只能选择与0节点相邻的节点,并且这些节点不能递增。
We can conclude that Kmin has to be between max and max + 2. 我们可以得出结论,Kmin必须在max和max + 2之间。
So an O(n) algorithm could look like this: 因此,O(n)算法可能如下所示:
max
and count how many nodes have that value => maxCount
找到最大节点值max
并计算有多少节点具有该值=> maxCount
maxCount = 1
then count how many nodes have the value max - 1
=> max1Count
, there are two possibilities: 如果maxCount = 1
则计算有多少个节点的值max - 1
=> max1Count
,有两种可能性:
max1Count = 0
or the number of adjacent nodes with value max - 1
to the node with value max
is equal to max1Count
=> the solution is max
max1Count = 0
或值的相邻节点的数量max - 1
与值的节点max
等于max1Count
=>该解决方案是max
max + 1
否则解为max + 1
max
value which are highest up in the tree: 找到树中最高的所有具有max
的节点:
max
: 如果在max
的第一次出现的深度处仅找到一个节点:
max
is equal to maxCount - 1
=> the solution is max + 1
值为max
的子maxCount - 1
等于maxCount - 1
=>解为max + 1
max
and the count of its children with value max
is equal to maxCount - 2
=> the solution is max + 1
too 只有一个孩子值max
和其子与值的计数max
等于maxCount - 2
=>该解决方案是max + 1
太 max
but there are maxCount - 1
grand children which all have the same parent and value max
=> the solution is max + 1
also 没有子项具有max
值,但是有maxCount - 1
它们都具有相同的父级,并且值max
=>解决方案也是max + 1
max + 2
否则解决方案为max + 2
maxCount
nodes and they all have the same parent => the solution is max + 1
如果找到了maxCount
节点并且它们都具有相同的父节点=>解决方案为max + 1
max + 2
否则解决方案为max + 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.