D. Masha and Cactus

Codeforces
IDCF856D
Time2000ms
Memory256MB
Difficulty
dptrees
English · Original
Chinese · Translation
Formal · Original
Masha is fond of cacti. When she was a little girl, she decided to plant a tree. Now Masha wants to make a nice cactus out of her tree. Recall that tree is a connected undirected graph that has no cycles. Cactus is a connected undirected graph such that each vertex belongs to at most one cycle. Masha has some additional edges that she can add to a tree. For each edge she knows which vertices it would connect and the beauty of this edge. Masha can add some of these edges to the graph if the resulting graph is a cactus. Beauty of the resulting cactus is sum of beauties of all added edges. Help Masha find out what maximum beauty of the resulting cactus she can achieve. ## Input The first line of the input data contains two integers _n_ and _m_ — the number of vertices in a tree, and the number of additional edges available (3 ≤ _n_ ≤ 2·105; 0 ≤ _m_ ≤ 2·105). Let us describe Masha's tree. It has a root at vertex 1. The second line contains _n_ - 1 integers: _p_2, _p_3, ..., _p__n_, here _p__i_ — is the parent of a vertex _i_ — the first vertex on a path from the vertex _i_ to the root of the tree (1 ≤ _p__i_ < _i_). The following _m_ lines contain three integers _u__i_, _v__i_ and _c__i_ — pairs of vertices to be connected by the additional edges that Masha can add to the tree and beauty of edge (1 ≤ _u__i_, _v__i_ ≤ _n_; _u__i_ ≠ _v__i_; 1 ≤ _c__i_ ≤ 104). It is guaranteed that no additional edge coincides with the edge of the tree. ## Output Output one integer — the maximum beauty of a cactus Masha can achieve. [samples]
Masha 喜欢仙人掌。当她还是个小女孩时,她种了一棵树。现在 Masha 想把她这棵树变成一个漂亮的仙人掌。 回忆一下,#cf_span(class=[tex-font-style-underline], body=[tree]) 是一个没有环的连通无向图。#cf_span(class=[tex-font-style-underline], body=[Cactus]) 是一个连通无向图,其中每个顶点至多属于一个环。 Masha 有一些额外的边可以添加到这棵树上。对于每条边,她知道它会连接哪两个顶点,以及这条边的 #cf_span(class=[tex-font-style-underline], body=[beauty])。Masha 可以选择添加其中一些边,前提是最终图是一个仙人掌。最终仙人掌的 #cf_span(class=[tex-font-style-underline], body=[Beauty]) 是所有添加边的 beauty 之和。 请帮助 Masha 找出她能获得的最大 #cf_span(class=[tex-font-style-underline], body=[beauty])。 输入的第一行包含两个整数 #cf_span[n] 和 #cf_span[m] —— 树的顶点数和可用的额外边数(#cf_span[3 ≤ n ≤ 2·105];#cf_span[0 ≤ m ≤ 2·105])。 我们来描述 Masha 的树。它的根在顶点 1。第二行包含 #cf_span[n - 1] 个整数:#cf_span[p2, p3, ..., pn],其中 #cf_span[pi] 是顶点 #cf_span[i] 的父节点,即从顶点 #cf_span[i] 到根(顶点 1)的路径上的第一个顶点(#cf_span[1 ≤ pi < i])。 接下来的 #cf_span[m] 行每行包含三个整数 #cf_span[ui]、#cf_span[vi] 和 #cf_span[ci] —— 表示 Masha 可以添加到树中的额外边所连接的顶点对及其 #cf_span(class=[tex-font-style-underline], body=[beauty])(#cf_span[1 ≤ ui, vi ≤ n];#cf_span[ui ≠ vi];#cf_span[1 ≤ ci ≤ 104])。 保证没有额外边与树中的边重合。 请输出一个整数 —— Masha 能获得的仙人掌的最大 #cf_span(class=[tex-font-style-underline], body=[beauty])。 ## Input 输入的第一行包含两个整数 #cf_span[n] 和 #cf_span[m] —— 树的顶点数和可用的额外边数(#cf_span[3 ≤ n ≤ 2·105];#cf_span[0 ≤ m ≤ 2·105])。我们来描述 Masha 的树。它的根在顶点 1。第二行包含 #cf_span[n - 1] 个整数:#cf_span[p2, p3, ..., pn],其中 #cf_span[pi] 是顶点 #cf_span[i] 的父节点,即从顶点 #cf_span[i] 到根(顶点 1)的路径上的第一个顶点(#cf_span[1 ≤ pi < i])。接下来的 #cf_span[m] 行每行包含三个整数 #cf_span[ui]、#cf_span[vi] 和 #cf_span[ci] —— 表示 Masha 可以添加到树中的额外边所连接的顶点对及其 #cf_span(class=[tex-font-style-underline], body=[beauty])(#cf_span[1 ≤ ui, vi ≤ n];#cf_span[ui ≠ vi];#cf_span[1 ≤ ci ≤ 104])。保证没有额外边与树中的边重合。 ## Output 请输出一个整数 —— Masha 能获得的仙人掌的最大 #cf_span(class=[tex-font-style-underline], body=[beauty])。 [samples]
**Definitions:** - Let $ T = (V, E_T) $ be a tree with $ |V| = n $, rooted at vertex 1, and $ E_T $ consisting of parent-child edges defined by $ p_2, p_3, \dots, p_n $. - Let $ A = \{ (u_i, v_i, c_i) \}_{i=1}^m $ be a set of $ m $ additional undirected edges, each with a beauty value $ c_i > 0 $, not present in $ E_T $. - A **cactus** is a connected undirected graph in which each vertex belongs to at most one simple cycle. **Constraints:** - The resulting graph $ G = (V, E_T \cup E_A) $, where $ E_A \subseteq A $, must be a cactus. - Each additional edge $ (u, v) \in E_A $ creates exactly one cycle in $ G $, which must consist of the edge $ (u, v) $ and the unique path between $ u $ and $ v $ in $ T $. - For any two distinct added edges $ e_1 = (u_1, v_1) $, $ e_2 = (u_2, v_2) $, the cycles they form in $ G $ must be **edge-disjoint** and **vertex-disjoint except possibly at cut vertices** (i.e., no two cycles share an edge; vertices may be shared only if they are articulation points not belonging to more than one cycle). **Objective:** Maximize the total beauty: $$ \sum_{(u,v,c) \in E_A} c $$ subject to the constraint that $ G = (V, E_T \cup E_A) $ is a cactus. **Equivalent Reformulation:** Each additional edge $ e = (u, v) $ defines a unique fundamental cycle $ C_e $ in $ T \cup \{e\} $, formed by $ e $ and the unique $ u $-$ v $ path in $ T $. Let $ \mathcal{C} = \{ C_1, C_2, \dots, C_m \} $ be the set of all such cycles induced by the $ m $ additional edges. The problem reduces to: **Select a maximum-weight subset of cycles $ \mathcal{C}' \subseteq \mathcal{C} $** such that no two cycles in $ \mathcal{C}' $ share an edge. This is equivalent to the **Maximum Weight Edge-Disjoint Cycle Selection** problem on a tree with additional edges, where each cycle is defined by an additional edge and the tree path. **Note:** Since the underlying graph is a tree, any two cycles $ C_i, C_j $ share an edge if and only if their corresponding tree paths overlap on at least one tree edge. Thus, the problem becomes selecting a maximum-weight subset of the $ m $ additional edges such that the corresponding tree paths are **edge-disjoint**. --- **Final Formal Statement:** Given: - A tree $ T = (V, E_T) $, $ |V| = n $, rooted at 1, with parent array $ p_2, \dots, p_n $. - A set $ A = \{ (u_i, v_i, c_i) \}_{i=1}^m $ of additional edges, each with weight $ c_i $. - For each $ e_i = (u_i, v_i) \in A $, define $ P_i $ as the unique simple path between $ u_i $ and $ v_i $ in $ T $. Find: $$ \max \sum_{i \in S} c_i $$ subject to: $$ \forall i, j \in S,\ i \ne j,\quad P_i \cap P_j = \emptyset \quad \text{(as edge sets)} $$ where $ S \subseteq \{1, 2, \dots, m\} $.
Samples
Input #1
7 3
1 1 2 2 3 3
4 5 1
6 7 1
2 3 1
Output #1
2
API Response (JSON)
{
  "problem": {
    "name": "D. Masha and Cactus",
    "description": {
      "content": "Masha is fond of cacti. When she was a little girl, she decided to plant a tree. Now Masha wants to make a nice cactus out of her tree. Recall that tree is a connected undirected graph that has no cy",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF856D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Masha is fond of cacti. When she was a little girl, she decided to plant a tree. Now Masha wants to make a nice cactus out of her tree.\n\nRecall that tree is a connected undirected graph that has no cy...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Masha 喜欢仙人掌。当她还是个小女孩时,她种了一棵树。现在 Masha 想把她这棵树变成一个漂亮的仙人掌。\n\n回忆一下,#cf_span(class=[tex-font-style-underline], body=[tree]) 是一个没有环的连通无向图。#cf_span(class=[tex-font-style-underline], body=[Cactus]) 是一个连通无向图,其中...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions:**\n\n- Let $ T = (V, E_T) $ be a tree with $ |V| = n $, rooted at vertex 1, and $ E_T $ consisting of parent-child edges defined by $ p_2, p_3, \\dots, p_n $.\n- Let $ A = \\{ (u_i, v_i, c_i...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments