CF1611D.Weights Assignment For Tree Edges

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a rooted tree consisting of nn vertices. Vertices are numbered from 11 to nn. Any vertex can be the root of a tree.

A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root.

The tree is specified by an array of ancestors bb containing nn numbers: bib_i is an ancestor of the vertex with the number ii. The ancestor of a vertex uu is a vertex that is the next vertex on a simple path from uu to the root. For example, on the simple path from 55 to 33 (the root), the next vertex would be 11, so the ancestor of 55 is 11.

The root has no ancestor, so for it, the value of bib_i is ii (the root is the only vertex for which bi=ib_i=i).

For example, if n=5n=5 and b=[3,1,3,3,1]b=[3, 1, 3, 3, 1], then the tree looks like this.

An example of a rooted tree for n=5n=5, the root of the tree is a vertex number 33.

You are given an array pp — a permutation of the vertices of the tree. If it is possible, assign any positive integer weights on the edges, so that the vertices sorted by distance from the root would form the given permutation pp.

In other words, for a given permutation of vertices pp, it is necessary to choose such edge weights so that the condition dist[pi]<dist[pi+1]dist[p_i] \lt dist[p_{i+1}] is true for each ii from 11 to n−1n-1. dist[u]dist[u] is a sum of the weights of the edges on the path from the root to uu. In particular, dist[u]=0dist[u]=0 if the vertex uu is the root of the tree.

For example, assume that p=[3,1,2,5,4]p=[3, 1, 2, 5, 4]. In this case, the following edge weights satisfy this permutation:

  • the edge (3,43, 4) has a weight of 102102;
  • the edge (3,13, 1) has weight of 11;
  • the edge (1,21, 2) has a weight of 1010;
  • the edge (1,51, 5) has a weight of 100100.

The array of distances from the root looks like: dist=[1,11,0,102,101]dist=[1,11,0,102,101]. The vertices sorted by increasing the distance from the root form the given permutation pp.

Print the required edge weights or determine that there is no suitable way to assign weights. If there are several solutions, then print any of them.

你被给定一棵包含 nn 个顶点的有根树。顶点编号为 11 到 nn。任意顶点均可作为树的根。

树是无环的连通无向图;有根树是选定一个顶点作为根的树。

该树由一个祖先数组 bb(含 nn 个整数)给出:bib_i 表示编号为 ii 的顶点的一个祖先。顶点 uu 的祖先,是指从 uu 到根的简单路径上紧邻 uu 的下一个顶点。例如,在从 55 到 33(根)的简单路径上,紧邻 55 的下一个顶点是 11,因此 55 的祖先为 11。

根没有祖先,故对根顶点 ii,其 bi=ib_i = i(根是唯一满足 bi=ib_i = i 的顶点)。

例如,若 n=5n=5 且 b=[3,1,3,3,1]b=[3, 1, 3, 3, 1],则该树如下图所示:

一个 n=5n=5 的有根树示例,树的根为顶点 33。

你被给定一个数组 pp —— 树中所有顶点的一个排列。若可能,请为每条边赋予任意正整数权重,使得按到根的距离升序排列的顶点序列恰好为给定排列 pp。

换言之,对于给定的顶点排列 pp,需选择边权,使得对每个 ii(从 11 到 n−1n-1),均有 dist[pi]<dist[pi+1]dist[p_i] \lt dist[p_{i+1}] 成立。其中 dist[u]dist[u] 表示从根到 uu 的路径上所有边的权重之和。特别地,若顶点 uu 即为根,则 dist[u]=0dist[u] = 0。

例如,设 p=[3,1,2,5,4]p=[3, 1, 2, 5, 4]。此时以下边权满足该排列:

  • 边 (3,4)(3, 4) 的权重为 102102;
  • 边 (3,1)(3, 1) 的权重为 11;
  • 边 (1,2)(1, 2) 的权重为 1010;
  • 边 (1,5)(1, 5) 的权重为 100100。

此时各顶点到根的距离数组为:dist=[1,11,0,102,101]dist=[1,11,0,102,101]。将顶点按到根距离升序排列,得到的序列即为所给排列 pp。

请输出满足要求的边权,或判定不存在可行的赋权方案。若存在多种解,输出任意一种即可。

输入格式

The first line of input data contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of input data sets in the test.

Each test case consists of three lines.

The first of them contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5). It is the number of vertices in the tree.

The second line contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n (1≤bi≤n1 \le b_i \le n). It is guaranteed that the bb array encodes some rooted tree.

The third line contains the given permutation pp: nn of different integers p1,p2,…,pnp_1, p_2, \dots, p_n (1≤pi≤n1 \le p_i \le n).

It is guaranteed that the sum of the values nn over all test cases in the test does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试中输入数据组的数量。

每组测试用例由三行组成。

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示树中顶点的数量。

第二行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n(1≤bi≤n1 \le b_i \le n)。保证数组 bb 编码了一棵有根树。

第三行给出给定的排列 pp:nn 个互不相同的整数 p1,p2,…,pnp_1, p_2, \dots, p_n(1≤pi≤n1 \le p_i \le n)。

保证测试中所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each set of input data print the answer on a separate line.

If the solution exists, print an array of nn integers w1,w2,…,wnw_1, w_2, \dots, w_n, where wiw_i is the weight of the edge that leads from bib_i to ii. For the root there is no such edge, so use the value wi=0w_i=0. For all other vertices, the values of wiw_i must satisfy the inequality 1≤wi≤1091 \le w_i \le 10^9. There can be equal numbers among wiw_i values, but all sums of weights of edges from the root to vertices must be different and satisfy the given permutation.

If there are several solutions, output any of them.

If no solution exists, output -1.

对于每组输入数据,请在单独的一行上输出答案。

如果解存在,则输出一个包含 nn 个整数的数组 w1,w2,…,wnw_1, w_2, \dots, w_n,其中 wiw_i 表示从 bib_i 指向 ii 的边的权重。对于根节点,不存在这样的边,因此令 wi=0w_i = 0。对于其余所有顶点,权重 wiw_i 必须满足不等式 1≤wi≤1091 \le w_i \le 10^9。wiw_i 的值可以有重复,但要求从根节点到各顶点的路径上所有边的权重之和互不相同,并恰好构成给定的排列。

若存在多个解,输出任意一个即可。

若无解,则输出 -1。

输入输出样例

  • 输入#1

    4
    5
    3 1 3 3 1
    3 1 2 5 4
    3
    1 1 2
    3 1 2
    7
    1 1 2 3 4 5 6
    1 2 3 4 5 6 7
    6
    4 4 4 4 1 1
    4 2 1 5 6 3

    输出#1

    1 10 0 102 100
    -1
    0 3 100 1 1 2 4
    6 5 10 0 2 3

说明/提示

The first set of input data of the example is analyzed in the main part of the statement.

In the second set of input data of the example, it is impossible to assign the positive weights to obtain a given permutation of vertices.

示例的第一组输入数据在题面主体部分中进行了分析。

在示例的第二组输入数据中,无法通过赋予正权重来得到给定的顶点排列。

输入解题思路,AI测评打分。不知道怎么写?

首页