Halftime Changes

Time limit: 10s Memory limit: 512MB Input: Output:

Task

You are given a tree with NN nodes, with root in node 11, and two arrays AA and BB, having NN elements, indexed from 11 to NN.

It is guaranteed that each level of the tree contains at most 100100 nodes.

You have to select some nodes YY for which it is possible to choose:

  • a subset S(Y)S(Y) of nodes on the path from the root to YY; S(Y) can be empty
  • a node X(Y)X(Y) that is a proper ancestor of both YY and all the nodes from S(Y)S(Y).

Such as the following condition holds:

A[X(Y)]vS(Y)A[v]=B[Y]A[X(Y)] - \sum_{v \in S(Y)}{A[v]} = B[Y]

A proper ancestor of VV is any node UU such that node UU is an ancestor of node VV and UU is not the same node as VV.

Before solving the problem, just like Nea' Gigi, the owner of FCSB, often makes miraculous changes at halftime, you are allowed to make changes. You can change at most KK elements of the array AA. The changed values are at least 11 and at most 10710^7.

Take note that KK is not given in the input. In the description of the subtasks it is specified the value of KK depending on NN.

After the changes are made and the nodes are selected as specified in the above statement, let MM be the number of selected nodes YY.

You will be scored based on how large the number MM is.
Note: You are not required to find the maximum possible value of MM. You will get the points for a subtask if MM meets the condition specified in the subtask description.

Input data

The first line contains the number NN. The second line contains N1N-1 values, where the ii-th value represents the parent of node i+1i+1. The third line contains NN integers, representing the array AA. The last line contains NN integers, representing the array BB.

Output data

The first line contains the elements of the array AA after the changes, in order from 11 to NN. The second line contains an integer MM, representing the number of nodes YY that satisfy the given condition. The following MM lines contains data about chosen nodes YY.
Each of these MM lines has the following format:

  • The node YY which satisfies the condition.
  • The chosen proper ancestor node X(Y)X(Y).
  • The size of the selected subset S(Y)S(Y) of nodes (can be 00).
  • The nodes in the subset S(Y)S(Y) (if any).

The values on the same line in the output are separated by a single space.

Constraints and clarifications

  • 1N400 0001 \leq N \leq 400 \ 000
  • 1Ai,BiN,(1iN)1\leq A_i, B_i \leq N, (1 \leq i \leq N)
  • The solution is not unique and you will be scored according to the subtasks description.
# Points Restrictions
1 12.5 N=5 000N= 5 \ 000, K=1 500K =1 \ 500, 10M10 \leq M
2 4 N=5 000N= 5 \ 000, K=1 500K =1 \ 500, 70M70 \leq M
3 8.5 N=5 000N= 5 \ 000, K=1 500K =1 \ 500, 500M500 \leq M
4 25 N=5 000N= 5 \ 000, K=1 500K =1 \ 500, 3500M3500 \leq M
5 12.5 N=400 000N= 400 \ 000, K=3 000K =3 \ 000, 20M20 \leq M
6 4 N=400 000N= 400 \ 000, K=3 000K =3 \ 000, 1000M1000 \leq M
7 8.5 N=400 000N= 400 \ 000, K=3 000K =3 \ 000, 80 000M80 \ 000 \leq M
8 25 N=400 000N= 400 \ 000, K=3 000K =3 \ 000, 397 000M397 \ 000 \leq M

Example

stdin

6
1 1 2 2 3
3 2 1 2 3 2
1 5 2 3 2 5

stdout

10 5 1 2 3 2
3
2 1 1 2
4 2 1 4
5 1 2 2 5

Explanation

In the array AA, 33 changes were made for positions 11, 22, and 33.

The number of nodes YY that satisfy the condition after the changes is M=3M=3.

For example, one of the selected nodes is Y=2Y=2, X(2)=1X(2) = 1 and the subset S(Y)S(Y) has 1 node, S(2)={2}S(2) = \{2\}. It can be observed that A[1]A[2]=105=5=B[2]A[1] - A[2] = 10 - 5 = 5 = B[2].

Another selected node is Y=5Y=5, X(5)=1X(5) = 1 and the subset S(Y)S(Y) has 2 nodes, S(5)={2,5}S(5) = \{2, 5\}. It can be observed that A[1](A[2]+A[5])=10(5+3)=2=B[5]A[1] - (A[2] + A[5]) = 10 - (5 + 3) = 2 = B[5].

Log in or sign up to be able to send submissions!