简体   繁体   English

通过在每一步增加相邻节点,找到使所有树节点为零所需的最小值

[英]Find minimum value needed to make all tree nodes zero by incrementing asjacent nodes at each step

  1. The problem is about finding the minimum value needed to make all nodes zero, let's call it K. 问题在于找到使所有节点为零所需的最小值,我们称其为K。

  2. A non-binary tree of numbers is given. 给出了一个非二进制数字树。

  3. 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. 请注意,一旦节点的值变为零,就不会再增加它,也不会允许与其连接的节点增加。

  4. Then you should choose another node which has at least one zero-valued node at distance one, and repeat the process 然后,您应该选择另一个在距离1处具有至少一个零值节点的节点,然后重复该过程

Example: 例:

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

My current approach: 我目前的做法:

  1. Find the max node and start from it (if there is more than one max node, the one which comes sooner is chosen) 找到最大节点并从中开始(如果有多个最大节点,则选择较早的那个)

  2. I define an array, let's call it possible nodes , and I add the node found in step 1 to it. 我定义了一个数组,我们将其称为可能的节点 ,然后向其中添加在步骤1中找到的节点。

  3. 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. 它给出错误的答案并达到超时限制。

Constraints 约束

  • 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为以下各项的最大值:

  1. the start node 起始节点
  2. its children + 1 它的孩子+1
  3. its parent + 1 其父+ 1
  4. all other nodes + 2 所有其他节点+ 2

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)算法可能如下所示:

  1. find the maximum node value max and count how many nodes have that value => maxCount 找到最大节点值max并计算有多少节点具有该值=> maxCount
  2. if maxCount = 1 then count how many nodes have the value max - 1 => max1Count , there are two possibilities: 如果maxCount = 1则计算有多少个节点的值max - 1 => max1Count ,有两种可能性:
    1. 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
    2. otherwise the solution is max + 1 否则解为max + 1
  3. find all the nodes with max value which are highest up in the tree: 找到树中最高的所有具有max的节点:
    1. if you found only one node for the depth of the first occurrence of max : 如果在max的第一次出现的深度处仅找到一个节点:
      1. the number of children with value max is equal to maxCount - 1 => the solution is max + 1 值为max的子maxCount - 1等于maxCount - 1 =>解为max + 1
      2. there is only one child with value 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
      3. there is no child with value 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
      4. otherwise the solution is max + 2 否则解决方案为max + 2
    2. if you found maxCount nodes and they all have the same parent => the solution is max + 1 如果找到了maxCount节点并且它们都具有相同的父节点=>解决方案为max + 1
    3. otherwise the solution is max + 2 否则解决方案为max + 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找访问树的所有节点的最低成本 - Find minimum cost to visit all nodes of a tree 请建议一些算法来查找树中的节点,该节点与所有节点之间的最远节点的距离最小 - Please suggest some algorithm to find the node in a tree whose distance to its farthest node is minimum among all the nodes 查找树中间附近的所有节点 - Find all nodes near the middle of the tree 如何在最小生成树上找到两个节点之间的路径 - How to find path between two nodes on a minimum spanning tree 二叉树中最低级别的所有叶节点的总和 - Sum of all leaf nodes at minimum level in binary tree 如果每个节点是其下所有节点的权重之和,则在树中找到最大权重节点。 - Find the maximum weight node in a tree if each node is the sum of the weights all the nodes under it. 查找可以服务我所有项目的最小节点数 - Find minimum number of nodes that can serve all my items 查找图的最小树型网络,其中每个节点相互连接,并找到每个节点与所有其他节点的总和 - Finding a minimal tree-type network of a graph where each node is connected to each other and find the sum of each node to all the other nodes 树中两个节点之间的最小距离 - Minimum distance between two nodes in tree 如何比较minHeapify中的节点以获取最小生成树? - How to compare nodes in minHeapify for minimum spanning tree?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM