E. Alternating Tree

Codeforces
IDCF960E
Time2000ms
Memory256MB
Difficulty
combinatoricsdfs and similardivide and conquerdpprobabilitiestrees
English · Original
Chinese · Translation
Formal · Original
Given a tree with $n$ nodes numbered from $1$ to $n$. Each node $i$ has an associated value $V_i$. If the simple path from $u_1$ to $u_m$ consists of $m$ nodes namely $u_1 \rightarrow u_2 \rightarrow u_3 \rightarrow \dots u_{m-1} \rightarrow u_{m}$, then its alternating function $A(u_{1},u_{m})$ is defined as $A(u_{1},u_{m}) = \sum\limits_{i=1}^{m} (-1)^{i+1} \cdot V_{u_{i}}$. A path can also have $0$ edges, i.e. $u_{1}=u_{m}$. Compute the sum of alternating functions of all unique simple paths. Note that the paths are directed: two paths are considered different if the starting vertices differ or the ending vertices differ. The answer may be large so compute it modulo $10^{9}+7$. ## Input The first line contains an integer $n$ $(2 \leq n \leq 2\cdot10^{5} )$ — the number of vertices in the tree. The second line contains $n$ space-separated integers $V_1, V_2, \ldots, V_n$ ($-10^9\leq V_i \leq 10^9$) — values of the nodes. The next $n-1$ lines each contain two space-separated integers $u$ and $v$ $(1\leq u, v\leq n, u \neq v)$ denoting an edge between vertices $u$ and $v$. It is guaranteed that the given graph is a tree. ## Output Print the total sum of alternating functions of all unique simple paths modulo $10^{9}+7$. [samples] ## Note Consider the first example. A simple path from node $1$ to node $2$: $1 \rightarrow 2$ has alternating function equal to $A(1,2) = 1 \cdot (-4)+(-1) \cdot 1 = -5$. A simple path from node $1$ to node $3$: $1 \rightarrow 3$ has alternating function equal to $A(1,3) = 1 \cdot (-4)+(-1) \cdot 5 = -9$. A simple path from node $2$ to node $4$: $2 \rightarrow 1 \rightarrow 4$ has alternating function $A(2,4) = 1 \cdot (1)+(-1) \cdot (-4)+1 \cdot (-2) = 3$. A simple path from node $1$ to node $1$ has a single node $1$, so $A(1,1) = 1 \cdot (-4) = -4$. Similarly, $A(2, 1) = 5$, $A(3, 1) = 9$, $A(4, 2) = 3$, $A(1, 4) = -2$, $A(4, 1) = 2$, $A(2, 2) = 1$, $A(3, 3) = 5$, $A(4, 4) = -2$, $A(3, 4) = 7$, $A(4, 3) = 7$, $A(2, 3) = 10$, $A(3, 2) = 10$. So the answer is $(-5) + (-9) + 3 + (-4) + 5 + 9 + 3 + (-2) + 2 + 1 + 5 + (-2) + 7 + 7 + 10 + 10 = 40$. Similarly $A(1,4)=-2, A(2,2)=1, A(2,1)=5, A(2,3)=10, A(3,3)=5, A(3,1)=9, A(3,2)=10, A(3,4)=7, A(4,4)=-2, A(4,1)=2, A(4,2)=3 , A(4,3)=7$ which sums upto 40.
给定一棵包含 $n$ 个节点的树,节点编号为 $1$ 到 $n$。每个节点 $i$ 有一个关联值 $V_i$。 如果从 $u_1$ 到 $u_m$ 的简单路径包含 $m$ 个节点,即 $u_1 \to u_2 \to u_3 \to \dots \to u_{m-1} \to u_m$,则其交错和 $A(u_1, u_m)$ 定义为 $A(u_1, u_m) = \sum_{i=1}^m (-1)^{i+1} \cdot V_{u_i}$。路径也可以包含 $0$ 条边,即 $u_1 = u_m$。 计算所有唯一简单路径的交错和之和。注意路径是有向的:如果起点不同或终点不同,则两条路径被视为不同。答案可能很大,因此请对 $10^9 + 7$ 取模。 第一行包含一个整数 $n$ $(2 \leq n \leq 2 \cdot 10^5)$ —— 树中顶点的数量。 第二行包含 $n$ 个用空格分隔的整数 $V_1, V_2, \dots, V_n$ ($-10^9 \leq V_i \leq 10^9$) —— 节点的值。 接下来的 $n - 1$ 行每行包含两个用空格分隔的整数 $u$ 和 $v$ $(1 \leq u, v \leq n, u \neq v)$,表示节点 $u$ 和 $v$ 之间有一条边。保证给定的图是一棵树。 请输出所有唯一简单路径的交错和之和对 $10^9 + 7$ 取模的结果。 考虑第一个例子。 从节点 $1$ 到节点 $2$ 的简单路径:$1 \to 2$ 的交错和为 $A(1, 2) = 1 \cdot (-4) + (-1) \cdot 1 = -5$。 从节点 $1$ 到节点 $3$ 的简单路径:$1 \to 3$ 的交错和为 $A(1, 3) = 1 \cdot (-4) + (-1) \cdot 5 = -9$。 从节点 $2$ 到节点 $4$ 的简单路径:$2 \to 1 \to 4$ 的交错和为 $A(2, 4) = 1 \cdot (1) + (-1) \cdot (-4) + 1 \cdot (-2) = 3$。 从节点 $1$ 到节点 $1$ 的路径只有一个节点 $1$,因此 $A(1, 1) = 1 \cdot (-4) = -4$。 类似地,$A(2, 1) = 5$,$A(3, 1) = 9$,$A(4, 2) = 3$,$A(1, 4) = -2$,$A(4, 1) = 2$,$A(2, 2) = 1$,$A(3, 3) = 5$,$A(4, 4) = -2$,$A(3, 4) = 7$,$A(4, 3) = 7$,$A(2, 3) = 10$,$A(3, 2) = 10$。因此答案为 $(-5) + (-9) + 3 + (-4) + 5 + 9 + 3 + (-2) + 2 + 1 + 5 + (-2) + 7 + 7 + 10 + 10 = 40$。 类似地,$A(1, 4) = -2$,$A(2, 2) = 1$,$A(2, 1) = 5$,$A(2, 3) = 10$,$A(3, 3) = 5$,$A(3, 1) = 9$,$A(3, 2) = 10$,$A(3, 4) = 7$,$A(4, 4) = -2$,$A(4, 1) = 2$,$A(4, 2) = 3$,$A(4, 3) = 7$,其和为 40。 ## Input 第一行包含一个整数 $n$ $(2 \leq n \leq 2 \cdot 10^5)$ —— 树中顶点的数量。第二行包含 $n$ 个用空格分隔的整数 $V_1, V_2, \dots, V_n$ ($-10^9 \leq V_i \leq 10^9$) —— 节点的值。接下来的 $n - 1$ 行每行包含两个用空格分隔的整数 $u$ 和 $v$ $(1 \leq u, v \leq n, u \neq v)$,表示节点 $u$ 和 $v$ 之间有一条边。保证给定的图是一棵树。 ## Output 请输出所有唯一简单路径的交错和之和对 $10^9 + 7$ 取模的结果。 [samples] ## Note 考虑第一个例子。从节点 $1$ 到节点 $2$ 的简单路径:$1 \to 2$ 的交错和为 $A(1, 2) = 1 \cdot (-4) + (-1) \cdot 1 = -5$。从节点 $1$ 到节点 $3$ 的简单路径:$1 \to 3$ 的交错和为 $A(1, 3) = 1 \cdot (-4) + (-1) \cdot 5 = -9$。从节点 $2$ 到节点 $4$ 的简单路径:$2 \to 1 \to 4$ 的交错和为 $A(2, 4) = 1 \cdot (1) + (-1) \cdot (-4) + 1 \cdot (-2) = 3$。从节点 $1$ 到节点 $1$ 的路径只有一个节点 $1$,因此 $A(1, 1) = 1 \cdot (-4) = -4$。类似地,$A(2, 1) = 5$,$A(3, 1) = 9$,$A(4, 2) = 3$,$A(1, 4) = -2$,$A(4, 1) = 2$,$A(2, 2) = 1$,$A(3, 3) = 5$,$A(4, 4) = -2$,$A(3, 4) = 7$,$A(4, 3) = 7$,$A(2, 3) = 10$,$A(3, 2) = 10$。因此答案为 $(-5) + (-9) + 3 + (-4) + 5 + 9 + 3 + (-2) + 2 + 1 + 5 + (-2) + 7 + 7 + 10 + 10 = 40$。类似地,$A(1, 4) = -2$,$A(2, 2) = 1$,$A(2, 1) = 5$,$A(2, 3) = 10$,$A(3, 3) = 5$,$A(3, 1) = 9$,$A(3, 2) = 10$,$A(3, 4) = 7$,$A(4, 4) = -2$,$A(4, 1) = 2$,$A(4, 2) = 3$,$A(4, 3) = 7$,其和为 40。
**Definitions** Let $ n \in \mathbb{Z} $ be the number of nodes in a tree. Let $ V = (V_1, V_2, \dots, V_n) \in \mathbb{Z}^n $ be the vector of node values. Let $ T = (V, E) $ be a tree with node set $ \{1, 2, \dots, n\} $ and edge set $ E $ of size $ n-1 $. For any ordered pair of nodes $ (u, v) $, let $ P(u, v) = (u = u_1, u_2, \dots, u_m = v) $ denote the unique simple path from $ u $ to $ v $, where $ m \geq 1 $ is the number of nodes in the path. Define the **alternating function** of path $ P(u, v) $ as: $$ A(u, v) = \sum_{i=1}^{m} (-1)^{i+1} V_{u_i} $$ **Constraints** 1. $ 2 \leq n \leq 2 \cdot 10^5 $ 2. $ -10^9 \leq V_i \leq 10^9 $ for all $ i \in \{1, \dots, n\} $ 3. The graph is a tree (connected, acyclic, undirected). **Objective** Compute: $$ \sum_{u=1}^{n} \sum_{v=1}^{n} A(u, v) \mod (10^9 + 7) $$
Samples
Input #1
4
-4 1 5 -2
1 2
1 3
1 4
Output #1
40
Input #2
8
-2 6 -4 -4 -9 -3 -7 23
8 2
2 3
1 4
6 5
7 6
4 7
5 8
Output #2
4
API Response (JSON)
{
  "problem": {
    "name": "E. Alternating Tree",
    "description": {
      "content": "Given a tree with $n$ nodes numbered from $1$ to $n$. Each node $i$ has an associated value $V_i$. If the simple path from $u_1$ to $u_m$ consists of $m$ nodes namely $u_1 \\rightarrow u_2 \\rightarrow",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF960E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Given a tree with $n$ nodes numbered from $1$ to $n$. Each node $i$ has an associated value $V_i$.\n\nIf the simple path from $u_1$ to $u_m$ consists of $m$ nodes namely $u_1 \\rightarrow u_2 \\rightarrow...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "给定一棵包含 $n$ 个节点的树,节点编号为 $1$ 到 $n$。每个节点 $i$ 有一个关联值 $V_i$。\n\n如果从 $u_1$ 到 $u_m$ 的简单路径包含 $m$ 个节点,即 $u_1 \\to u_2 \\to u_3 \\to \\dots \\to u_{m-1} \\to u_m$,则其交错和 $A(u_1, u_m)$ 定义为 $A(u_1, u_m) = \\sum_{i=1}^m (-1...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the number of nodes in a tree.  \nLet $ V = (V_1, V_2, \\dots, V_n) \\in \\mathbb{Z}^n $ be the vector of node values.  \nLet $ T = (V, E) $ be a tree with nod...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments