CF1714G.Path Prefixes

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a rooted tree. It contains nn vertices, which are numbered from 11 to nn. The root is the vertex 11.

Each edge has two positive integer values. Thus, two positive integers aja_j and bjb_j are given for each edge.

Output n−1n-1 numbers r2,r3,…,rnr_2, r_3, \dots, r_n, where rir_i is defined as follows.

Consider the path from the root (vertex 11) to ii (2≤i≤n2 \le i \le n). Let the sum of the costs of aja_j along this path be AiA_i. Then rir_i is equal to the length of the maximum prefix of this path such that the sum of bjb_j along this prefix does not exceed AiA_i.

Example for n=9n=9. The blue color shows the costs of aja_j, and the red color shows the costs of bjb_j.

Consider an example. In this case:

  • r2=0r_2=0, since the path to 22 has an amount of aja_j equal to 55, only the prefix of this path of length 00 has a smaller or equal amount of bjb_j;
  • r3=3r_3=3, since the path to 33 has an amount of aja_j equal to 5+9+5=195+9+5=19, the prefix of length 33 of this path has a sum of bjb_j equal to 6+10+1=176+10+1=17 ( the number is 17≤1917 \le 19);
  • r4=1r_4=1, since the path to 44 has an amount of aja_j equal to 5+9=145+9=14, the prefix of length 11 of this path has an amount of bjb_j equal to 66 (this is the longest suitable prefix, since the prefix of length 22 already has an amount of bjb_j equal to 6+10=166+10=16, which is more than 1414);
  • r5=2r_5=2, since the path to 55 has an amount of aja_j equal to 5+9+2=165+9+2=16, the prefix of length 22 of this path has a sum of bjb_j equal to 6+10=166+10=16 (this is the longest suitable prefix, since the prefix of length 33 already has an amount of bjb_j equal to 6+10+1=176+10+1=17, what is more than 1616);
  • r6=1r_6=1, since the path up to 66 has an amount of aja_j equal to 22, the prefix of length 11 of this path has an amount of bjb_j equal to 11;
  • r7=1r_7=1, since the path to 77 has an amount of aja_j equal to 5+3=85+3=8, the prefix of length 11 of this path has an amount of bjb_j equal to 66 (this is the longest suitable prefix, since the prefix of length 22 already has an amount of bjb_j equal to 6+3=96+3=9, which is more than 88);
  • r8=2r_8=2, since the path up to 88 has an amount of aja_j equal to 2+4=62+4=6, the prefix of length 22 of this path has an amount of bjb_j equal to 1+3=41+3=4;
  • r9=3r_9=3, since the path to 99 has an amount of aja_j equal to 2+4+1=72+4+1=7, the prefix of length 33 of this path has a sum of bjb_j equal to 1+3+3=71+3+3=7.

你被给定一棵有根树,该树包含 nn 个顶点,编号从 11 到 nn,其中根节点为顶点 11。

每条边有两个正整数权值。因此,对每条边 jj,给出两个正整数 aja_j 和 bjb_j。

请输出 n−1n-1 个数 r2,r3,…,rnr_2, r_3, \dots, r_n,其中 rir_i 的定义如下:

考虑从根节点(顶点 11)到顶点 ii(2≤i≤n2 \le i \le n)的路径。设该路径上所有边的 aja_j 值之和为 AiA_i。则 rir_i 表示该路径的最长前缀长度,使得该前缀上所有边的 bjb_j 值之和不超过 AiA_i。

n=9n=9 的示例。蓝色表示 aja_j 的权值,红色表示 bjb_j 的权值。

我们来看一个例子。在该例中:

  • r2=0r_2=0,因为到顶点 22 的路径上 aja_j 的总和为 55,而该路径长度为 00 的前缀(即空路径)对应的 bjb_j 总和为 00(满足 ≤5\le 5),但长度为 11 的前缀的 bjb_j 总和已超过 55;
  • r3=3r_3=3,因为到顶点 33 的路径上 aja_j 的总和为 5+9+5=195+9+5=19,该路径长度为 33 的前缀的 bjb_j 总和为 6+10+1=176+10+1=17(满足 17≤1917 \le 19);
  • r4=1r_4=1,因为到顶点 44 的路径上 aja_j 的总和为 5+9=145+9=14,该路径长度为 11 的前缀的 bjb_j 总和为 66(这是满足条件的最长前缀,因为长度为 22 的前缀的 bjb_j 总和为 6+10=16>146+10=16 > 14);
  • r5=2r_5=2,因为到顶点 55 的路径上 aja_j 的总和为 5+9+2=165+9+2=16,该路径长度为 22 的前缀的 bjb_j 总和为 6+10=166+10=16(这是满足条件的最长前缀,因为长度为 33 的前缀的 bjb_j 总和为 6+10+1=17>166+10+1=17 > 16);
  • r6=1r_6=1,因为到顶点 66 的路径上 aja_j 的总和为 22,该路径长度为 11 的前缀的 bjb_j 总和为 11;
  • r7=1r_7=1,因为到顶点 77 的路径上 aja_j 的总和为 5+3=85+3=8,该路径长度为 11 的前缀的 bjb_j 总和为 66(这是满足条件的最长前缀,因为长度为 22 的前缀的 bjb_j 总和为 6+3=9>86+3=9 > 8);
  • r8=2r_8=2,因为到顶点 88 的路径上 aja_j 的总和为 2+4=62+4=6,该路径长度为 22 的前缀的 bjb_j 总和为 1+3=41+3=4;
  • r9=3r_9=3,因为到顶点 99 的路径上 aja_j 的总和为 2+4+1=72+4+1=7,该路径长度为 33 的前缀的 bjb_j 总和为 1+3+3=71+3+3=7。

输入格式

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

The descriptions of test cases follow.

Each description begins with a line that contains an integer nn (2≤n≤2⋅1052 \le n \le 2\cdot10^5) — the number of vertices in the tree.

This is followed by n−1n-1 string, each of which contains three numbers pj,aj,bjp_j, a_j, b_j (1≤pj≤n1 \le p_j \le n; 1≤aj,bj≤1091 \le a_j,b_j \le 10^9) — the ancestor of the vertex jj, the first and second values an edge that leads from pjp_j to jj. The value of jj runs through all values from 22 to nn inclusive. It is guaranteed that each set of input data has a correct hanged tree with a root at the vertex 11.

It is guaranteed that the sum of nn over all input test cases does not exceed 2⋅1052\cdot10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

随后是各测试用例的描述。

每个测试用例的描述以一行开始,该行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2\cdot10^5)—— 树中顶点的数量。

接下来是 n−1n-1 行字符串,每行包含三个数 pj,aj,bjp_j, a_j, b_j(1≤pj≤n1 \le p_j \le n;1≤aj,bj≤1091 \le a_j,b_j \le 10^9)—— 分别表示顶点 jj 的父节点、从 pjp_j 指向 jj 的边的第一个值和第二个值。其中 jj 取遍从 22 到 nn(含)的所有整数。保证每组输入数据均构成一棵以顶点 11 为根的合法有根树。

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

输出格式

For each test case, output n−1n-1 integer in one line: r2,r3,…,rnr_2, r_3, \dots, r_n.

对于每个测试用例,在一行中输出 n−1n-1 个整数:r2,r3,…,rnr_2, r_3, \dots, r_n。

输入输出样例

  • 输入#1

    4
    9
    1 5 6
    4 5 1
    2 9 10
    4 2 1
    1 2 1
    2 3 3
    6 4 3
    8 1 3
    4
    1 1 100
    2 1 1
    3 101 1
    4
    1 100 1
    2 1 1
    3 1 101
    10
    1 1 4
    2 3 5
    2 5 1
    3 4 3
    3 1 5
    5 3 5
    5 2 1
    1 3 2
    6 2 1

    输出#1

    0 3 1 2 1 1 2 3 
    0 0 3 
    1 2 2 
    0 1 2 1 1 2 2 1 1

说明/提示

The first example is clarified in the statement.

In the second example:

  • r2=0r_2=0, since the path to 22 has an amount of aja_j equal to 11, only the prefix of this path of length 00 has a smaller or equal amount of bjb_j;
  • r3=0r_3=0, since the path to 33 has an amount of aja_j equal to 1+1=21+1=2, the prefix of length 11 of this path has an amount of bjb_j equal to 100100 (100>2100 \gt 2);
  • r4=3r_4=3, since the path to 44 has an amount of aja_j equal to 1+1+101=1031+1+101=103, the prefix of length 33 of this path has an amount of bjb_j equal to 102102, .

第一个示例在题面中已作说明。

第二个示例中:

  • r2=0r_2=0,因为到达节点 22 的路径上 aja_j 的总和为 11,该路径中仅有长度为 00 的前缀满足其 bjb_j 的总和小于等于 11;
  • r3=0r_3=0,因为到达节点 33 的路径上 aja_j 的总和为 1+1=21+1=2,该路径中长度为 11 的前缀的 bjb_j 总和为 100100(100>2100 \gt 2);
  • r4=3r_4=3,因为到达节点 44 的路径上 aja_j 的总和为 1+1+101=1031+1+101=103,该路径中长度为 33 的前缀的 bjb_j 总和为 102102。

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

首页