QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100

#6160. 树

Statistics

给定一棵 $n$ 个结点的有根树 $T$,结点从 $1$ 开始编号,根结点为 $1$ 号结点,每个结点有一个正整数权值 $v_i$。

设 $x$ 号结点的子树内(包含 $x$ 自身)的所有结点编号为 $c_1, c_2, \dots, c_k$,定义 $x$ 的价值为:

$$ val(x) = (v_{c_1} + d(c_1, x)) \oplus (v_{c_2} + d(c_2, x)) \oplus \cdots \oplus (v_{c_k} + d(c_k, x)) $$

其中 $d(x, y)$ 表示树上 $x$ 号结点与 $y$ 号结点间唯一简单路径所包含的边数,$d(x, x) = 0$。$\oplus$ 表示异或运算。

请你求出 $\sum_{i=1}^n val(i)$ 的结果。

输入格式

第一行一个正整数 $n$ 表示树的大小。

第二行 $n$ 个正整数表示 $v_i$。

接下来一行 $n-1$ 个正整数,依次表示 $2$ 号结点到 $n$ 号结点,每个结点的父亲编号 $p_i$。

输出格式

仅一行一个整数表示答案。

样例数据

样例 1 输入

5
5 4 1 2 3
1 1 2 2

样例 1 输出

12

样例 1 解释

$val(1) = (5 + 0)\oplus(4 + 1)\oplus(1 + 1)\oplus(2 + 2)\oplus(3 + 2) = 3$。

$val(2) = (4 + 0)\oplus(2 + 1)\oplus(3 + 1) = 3$。

$val(3) = (1 + 0) = 1$。

$val(4) = (2 + 0) = 2$。

$val(5) = (3 + 0) = 3$。

和为 $12$。

样例 2

见下发文件。

子任务

$10\%$ 的数据:$1\le n\le 2\,501$。

$40\%$ 的数据:$1\le n\le 152\,501$。

另有 $20\%$ 的数据:所有 $p_i = i - 1 (2\le i\le n)$。

另有 $20\%$ 的数据:所有 $v_i = 1 (1\le i\le n)$。

$100\%$ 的数据:$1\le n, v_i \le 525\,010, 1\le p_i\le n$。

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.