CF BUDDY
← Problems·

1714G · Path Prefixes

1700 · binary search, data structures, dfs and similar

Problem: 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 n1n-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 (2in2 \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 171917 \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.

Input Format: The first line contains an integer tt (1t1041 \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 (2n21052 \le n \le 2\cdot10^5) — the number of vertices in the tree.

This is followed by n1n-1 string, each of which contains three numbers pj,aj,bjp_j, a_j, b_j (1pjn1 \le p_j \le n; 1aj,bj1091 \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 21052\cdot10^5.

Output Format: For each test case, output n1n-1 integer in one line: r2,r3,,rnr_2, r_3, \dots, r_n.

Note: 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 > 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, .

Sample Cases

Case 1

Input

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

Output

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

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback