#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 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 to denote the ID of the -th power strip, and to denote the power strip into which the plug of the -th power strip is inserted (i.e., the parent of in the tree). Let represent the live wire, the neutral wire, and the ground wire. Then the resistance of the whole network can be described by , which represents the resistance between wire of and wire of (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 of power strip and wire of power strip . The root node of the tree is not plugged into any other power strip, and its ID is .
Input Format
The first line contains a positive integer , the number of power strips.
The next lines are divided into blocks, each block consisting of 4 lines.
In the -th block, the first line contains an integer , meaning that the parent of power strip is .
Then follows a matrix . The number in row , column is the reciprocal of the resistance between wire of power strip and wire of power strip .
Then an integer is given, denoting the number of operations.
Each operation starts with an integer .
- If , then four integers follow, meaning to change the resistance between wire of power strip and wire of its parent to the reciprocal of . It is guaranteed that .
- If , then four integers follow, meaning to query the resistance between wire of power strip and wire of power strip . It is guaranteed that .
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. the reciprocal of each resistance in the input .
| Test point ID | Feature |
|---|---|
| , and the input contains only queries, no modifications | |
| , and the tree is a chain | |
| , and the height of the tree does not exceed | |
Translated by ChatGPT 5
京公网安备 11011102002149号