#P4504. [CTSC2014] 插线板

[CTSC2014] 插线板

Description

Allison has a lot of electronic devices, such as an iMac, iPod, iPhone, and iPad. So she plans to buy power strips to charge them. After doing a lot of online research, Allison found a beautiful and delicate Tianyi-brand power strip (as shown in the lower-left picture). At first sight, Allison was attracted by its exquisite design, so she bought nn power strips of this model at once.

But then a problem arose: Allison’s home has only one wall socket. She needs to connect power strips so that electricity can be carried out layer by layer (as shown in the lower-right picture).

The connection of power strips forms a tree structure: the plug of each power strip is inserted into a socket of another power strip (except the root node), and the connections are not allowed to form a cycle.

Each power strip has three wires: live, neutral, and ground. As the number of power strips increases and wires wear out, the contact resistance between wires in the circuit has become too significant to ignore.

How can we describe the tree structure of the power strips and the resistance relationships between wires? Allison came up with a mathematical model: use aia_i to denote the ID of the ii-th power strip, and fif_i to denote the power strip into which the plug of the ii-th power strip is inserted (i.e., the parent of aia_i in the tree). Let 11 represent the live wire, 22 the neutral wire, and 33 the ground wire. Then the resistance of the whole network can be described by R(ai,fi,x,y)(x,y{1,2,3})R(a_i,f_i,x,y)(x,y∈\{1,2,3\}), which represents the resistance between wire xx of aia_i and wire yy of fif_i (in this model, Allison assumes that the live wire and neutral wire may also be connected and produce resistance). An example is shown below:

As time passes, the resistance between wires may also change. Now Allison wants to know, in the current power-strip tree circuit, what the resistance is between wire xx of power strip aia_i and wire yy of power strip aja_j. The root node of the tree is not plugged into any other power strip, and its ID is 11.

Input Format

The first line contains a positive integer nn, the number of power strips.

The next 4(n1)4(n-1) lines are divided into blocks, each block consisting of 4 lines.

In the ii-th block, the first line contains an integer fi+1f_{i+1}, meaning that the parent of power strip i+1i+1 is fi+1f_{i+1}.

Then follows a 3×33\times3 matrix gxyg_{xy}. The number in row xx, column yy is the reciprocal of the resistance between wire xx of power strip i+1i+1 and wire yy of power strip fi+1f_{i+1}.

Then an integer qq is given, denoting the number of operations.

Each operation starts with an integer kk.

  • If k=1k=1, then four integers ai,xi,xj,ga_i,x_i,x_j,g follow, meaning to change the resistance between wire xix_i of power strip aia_i and wire xjx_j of its parent faif_{a_i} to the reciprocal of gg. It is guaranteed that 2ain2≤a_i≤n.
  • If k=2k=2, then four integers ai,xi,aj,xja_i,x_i,a_j,x_j follow, meaning to query the resistance between wire xix_i of power strip aia_i and wire xjx_j of power strip aja_j. It is guaranteed that aiaja_i≠a_j.

Output Format

For each query, output one line with a real number, representing the resistance between the two wires.

If $|(\text{your output}-\text{standard output})/\text{standard output}|≤10^{-3}$, then the resistance value is considered correct. If all resistance values are correct, you will get the score for that test point.

3
1
6 7 1 
4 1 8 
8 3 3 
2
7 5 8 
10 5 7 
10 5 5 
10
1 3 1 2 2
1 2 2 3 5
2 3 2 2 2
1 2 3 2 5
1 3 1 2 3
2 1 2 2 3
1 3 2 1 2
2 2 1 1 1
1 2 1 2 5
2 3 1 1 2
0.083836
0.095256
0.078828
0.146900

Hint

For all testdata, the input is guaranteed to be a tree. 0<0< the reciprocal of each resistance in the input 10≤10.

Test point ID Feature
161\sim6 n100,q1000n≤100,q≤1000, and the input contains only queries, no modifications
7107\sim10 n1000,q1000n≤1000,q≤1000, and the tree is a chain
111611\sim16 n10000,q10000n≤10000,q≤10000, and the height of the tree does not exceed 3030
172017\sim20 n10000,q10000n≤10000,q≤10000

Translated by ChatGPT 5