C. Garland

Codeforces
IDCF767C
Time2000ms
Memory256MB
Difficulty
dfs and similargraphsgreedytrees
English · Original
Formal · Original
Once at New Year Dima had a dream in which he was presented a fairy garland. A garland is a set of lamps, some pairs of which are connected by wires. Dima remembered that each two lamps in the garland were connected directly or indirectly via some wires. Furthermore, the number of wires was exactly one less than the number of lamps. There was something unusual about the garland. Each lamp had its own brightness which depended on the temperature of the lamp. Temperatures could be positive, negative or zero. Dima has two friends, so he decided to share the garland with them. He wants to cut two different wires so that the garland breaks up into three parts. Each part of the garland should shine equally, i. e. the sums of lamps' temperatures should be equal in each of the parts. Of course, each of the parts should be non-empty, i. e. each part should contain at least one lamp. <center>![image](https://espresso.codeforces.com/a5a55756585899132422c0496548a8185f2b9117.png)</center>Help Dima to find a suitable way to cut the garland, or determine that this is impossible. While examining the garland, Dima lifted it up holding by one of the lamps. Thus, each of the lamps, except the one he is holding by, is now hanging on some wire. So, you should print two lamp ids as the answer which denote that Dima should cut the wires these lamps are hanging on. Of course, the lamp Dima is holding the garland by can't be included in the answer. ## Input The first line contains single integer _n_ (3 ≤ _n_ ≤ 106) — the number of lamps in the garland. Then _n_ lines follow. The _i_\-th of them contain the information about the _i_\-th lamp: the number lamp _a__i_, it is hanging on (and 0, if is there is no such lamp), and its temperature _t__i_ ( - 100 ≤ _t__i_ ≤ 100). The lamps are numbered from 1 to _n_. ## Output If there is no solution, print _\-1_. Otherwise print two integers — the indexes of the lamps which mean Dima should cut the wires they are hanging on. If there are multiple answers, print any of them. [samples] ## Note The garland and cuts scheme for the first example: <center>![image](https://espresso.codeforces.com/46b2836774cafe0b5604e130026638f2ed2eb827.png)</center>
**Definitions:** - Let $ G = (V, E) $ be an undirected tree with $ n $ vertices (lamps), where $ |V| = n $, $ |E| = n - 1 $. - Each vertex $ i \in V $ has a weight (temperature) $ t_i \in \mathbb{Z} $, $ -100 \leq t_i \leq 100 $. - The total sum of weights is $ S = \sum_{i=1}^n t_i $. **Constraints:** - The tree is rooted arbitrarily (input gives parent-child structure; lamp 0 means no parent). - We seek to remove two distinct edges $ e_1, e_2 \in E $, such that the resulting forest consists of exactly three connected components, each with vertex weight sum equal to $ \frac{S}{3} $. - Each component must be non-empty: $ \frac{S}{3} \neq 0 $, and $ S \equiv 0 \pmod{3} $. - The two removed edges must not be incident to the root (the lamp Dima is holding); thus, the root cannot be an endpoint of either cut edge. **Objective:** Find two edges $ (u_1, p_{u_1}), (u_2, p_{u_2}) $, where $ u_1, u_2 \neq \text{root} $, such that removing them splits the tree into three subtrees, each with total weight $ \frac{S}{3} $. **Formal Conditions:** 1. $ S \equiv 0 \pmod{3} $. If not, output $-1$. 2. Let $ T_u $ denote the subtree rooted at $ u $ (including $ u $), and $ w(T_u) = \sum_{v \in T_u} t_v $. 3. We require two distinct non-root vertices $ u, v $ (i.e., $ u \neq \text{root}, v \neq \text{root} $) such that: - $ w(T_u) = \frac{S}{3} $, - $ w(T_v) = \frac{S}{3} $, - and $ T_u $ and $ T_v $ are disjoint (i.e., neither is a subtree of the other). **Output:** - If such $ u, v $ exist: output $ u, v $ (in any order). - Otherwise: output $-1$. **Note:** The root is determined as the unique node with parent $ 0 $. All cut edges are of the form $ (u, \text{parent}(u)) $, so the answer is the child endpoints of the cut edges.
Samples
Input #1
6
2 4
0 5
4 2
2 1
1 1
4 2
Output #1
1 4
Input #2
6
2 4
0 6
4 2
2 1
1 1
4 2
Output #2
\-1
API Response (JSON)
{
  "problem": {
    "name": "C. Garland",
    "description": {
      "content": "Once at New Year Dima had a dream in which he was presented a fairy garland. A garland is a set of lamps, some pairs of which are connected by wires. Dima remembered that each two lamps in the garland",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF767C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Once at New Year Dima had a dream in which he was presented a fairy garland. A garland is a set of lamps, some pairs of which are connected by wires. Dima remembered that each two lamps in the garland...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions:**\n\n- Let $ G = (V, E) $ be an undirected tree with $ n $ vertices (lamps), where $ |V| = n $, $ |E| = n - 1 $.\n- Each vertex $ i \\in V $ has a weight (temperature) $ t_i \\in \\mathbb{Z} ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments