#P10776. BZOJ3914 Jabby's shadows

BZOJ3914 Jabby's shadows

Problem Description

You are given an unrooted tree with nn nodes. The tree edges have weights. Each node has one of two colors. Initially, all nodes are black. Black is 11, and white is 22. Each edge has a positive weight.

You need to maintain mm operations:

  • 1 u: Query the diameter of the monochromatic connected component (connected block with the same color) that contains node uu. If it is 00, output QwQ.
  • 2 u v c: Recolor (cover) the path from uu to vv with color cc.

Input Format

The first line contains a positive integer nn, the number of nodes in the tree.

The second line contains n1n-1 positive integers fif_i, the parent of nodes 2n2 \sim n.

The third line contains n1n-1 positive integers eie_i, the edge weight of the edge from node 2n2 \sim n to its parent.

The fourth line contains a positive integer mm, the number of operations. The next mm lines describe the operations in order.

Output Format

For each operation of type 11, output one line as the answer.

5
1 2 3 3
2 2 4 3
5
1 3
1 1
2 4 4 2
2 3 1 1
1 2
8
8
7

Hint

Constraints: 1n,m1000001\leq n,m\leq 100000, 1ei100001\leq e_i\leq 10000.

Translated by ChatGPT 5