连接树中的节点

发布于 2024-10-04 10:33:34 字数 106 浏览 0 评论 0原文

作为一个假设问题,假设给定一棵树 T 和 T 中的一对节点 (x, y) 列表。我被问到使用每条边可以同时连接多少对节点(将 x 与 y 连接)在 T 中最多一次。

一个人会怎样做呢?

As a hypothetical question, say I'm given a tree T and a list of pair of nodes (x, y) in T. I'm asked how many of the pairs I can connect simultaneously (connect x with y) using each edge in T at most once.

How would one do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森罗 2024-10-11 10:33:34

对于树来说这不是 NP 困难的。例如,您可以执行以下操作。

  1. 任意给树生根。

  2. 对于每个顶点 v,计算仅限于 v 子树的最优解。

  3. 此外,对于每个 v,对于包含 v 父边的每个路径 p,计算仅限于除路径 p 之外的 v 子树的最优解。

(2) 和 (3) 可以使用 v 子树内较小问题的解来计算。

查看步骤 1、2 和 3,并自己计算出递归式可能更容易,但只是为了给您一个想法,可以通过取多个解的最大值来计算 (2):将v 的子级(即,步骤 2 中为每个子级生成的解决方案的总和),以及包含 v 的每条路径加上其他子级的步骤 2 解决方案(这基本上包括生成的一个或两个解决方案的总和)在步骤 3 中为 v 的子项加上步骤 2 中为其他子项生成的解的总和)。

It isn't NP-hard for trees. You can do the following, for example.

  1. Root the tree arbitrarily.

  2. For each vertex v, compute the optimal solution that is confined to v's subtree.

  3. Additionally, for each v, for each path p that includes v's parent edge, compute the optimal solution that is confined to v's subtree except for path p.

(2) and (3) can be computed using the solutions to the smaller problems within v's subtree.

It's probably easier to look at steps 1, 2, and 3, and figure out the recurrence yourself, but just to give you an idea, (2) can be computed by taking the maximum of several solutions: one that sums the solutions for the children of v (i.e., the sum of the solutions produced in step 2 for each child), and one for each path that includes v plus the step 2 solutions for the other children (this will essentially include the sum of one or two solutions produced in step 3 for v's children, plus the sum of the solutions produced in step 2 for the other children).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文