QOJ.ac

QOJ

Time Limit: 5 s Memory Limit: 256 MB Total points: 100

#7591. 延長線插座

Statistics

Allison 有非常多的電子裝置,例如 iMac、iPod、iPhone 和 iPad。所以她準備購買延長線插座來為電子裝置充電。做了大量的網路調查後,Allison 發現了一款美麗精巧的天翼牌延長線插座(如下方左圖所示)。看到這款延長線插座的第一眼,Allison 就被它精美的造型吸引,於是她一次購買了 $n$ 個這種型號的延長線插座。

problem_7591_d2faf3aaa7aab10beb4834a053023362.png problem_7591_28aed1864b2320c7fc4722884671bef2.png

可是問題也隨之而來,Allison 家中只有一個插座,她需要透過延長線插座的連接,將電一層一層地引出(如上方右圖所示)。

延長線插座的連接方式是樹狀結構:每個延長線插座的插頭插在另一個延長線插座的插孔中(根節點除外),連接不允許形成環。

每個延長線插座有火線、中性線、接地線三根導線。隨著延長線插座數量增加、導線磨損,電路中導線彼此接觸產生的電阻已經到了不能忽視的程度。

要如何描述延長線插座的樹狀結構,以及導線之間的電阻關係呢?Allison 想出了一個數學模型:用 $a_i$ 代表第 $i$ 個延長線插座的編號,$f_i$ 代表第 $i$ 個延長線插座的插頭所插入的延長線插座(即 $a_i$ 在樹中的父節點);$1$ 代表火線、$2$ 代表中性線、$3$ 代表接地線。則整個電路的電阻可以用 $R(a_i,f_i,x,y)$($x,y\in\{1,2,3\}$)描述,表示 $a_i$ 的 $x$ 線與 $f_i$ 的 $y$ 線之間的電阻值(在這個數學模型中,Allison 認為火線和中性線也可能連接並產生電阻)。以下是一個例子:

problem_7591_d0852c2754e64ca1c6aad1d781e43a86.png

隨著時間推移,導線之間的電阻也可能改變。現在,Allison 想知道在延長線插座的樹狀電路中,目前 $a_i$ 延長線插座的 $x$ 線與 $a_j$ 延長線插座的 $y$ 線之間的電阻是多少。規定樹的根節點不再插入其他延長線插座,且編號為 $1$。

輸入格式

輸入檔案 circuit.in 的第一行包含一個正整數 $n$,表示延長線插座的數量。

接下來 $4(n-1)$ 行,每 $4$ 行為一個區塊。

第 $i$ 個區塊的第一行是一個整數 $f_i$,表示編號為 $i+1$ 的延長線插座的父節點為 $f_i$ 延長線插座。

接下來是一個 $3\times3$ 矩陣 $g_{xy}$,第 $x$ 行第 $y$ 列表示編號為 $i+1$ 的延長線插座的 $x$ 線與 $f_i$ 的 $y$ 線之間電阻的倒數。

接下來是一個整數 $q$,表示有 $q$ 次操作。

接下來 $q$ 行,每行包含若干個整數。

第一個整數為 $k$。若 $k=1$,則接下來包含四個整數 $a_i,x_i,x_j,g$,表示將 $a_i$ 延長線插座的 $x_i$ 線與其父節點 $f_i$ 的 $x_j$ 線之間的電阻值改為 $g$ 的倒數。保證 $2\le a_i\le n$。

若 $k=2$,則接下來包含四個整數 $a_i,x_i,a_j,x_j$,表示查詢 $a_i$ 延長線插座的 $x_i$ 線與 $a_j$ 延長線插座的 $x_j$ 線之間的電阻值。保證 $a_i\ne a_j$。

輸出格式

輸出至檔案 circuit.out。

對每次查詢,輸出一行實數,表示兩條線之間的電阻。

若 $\left|\frac{\text{選手輸出}-\text{標準輸出}}{\text{標準輸出}}\right|<10^{-3}$,則該電阻值被視為正確。若所有電阻值都正確,則獲得該測試點的分數。

範例

輸入格式 1

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

輸出格式 1

0.083836
0.095256
0.078828
0.146900

範例 2

請見選手目錄下的 circuit/circuit.in 與 circuit/circuit.ans。

資料範圍

對所有測試資料,保證輸入是一棵樹,且 $0<\text{輸入中電阻的倒數}\le10$。

測試點編號 特點
$1\sim6$ $n\le100,q\le1000$,保證輸入中只有查詢、沒有修改
$7\sim10$ $n\le1000,q\le1000$,保證資料是一條鏈
$11\sim16$ $n\le10000,q\le10000$,保證樹的高度不超過 $30$
$17\sim20$ $n\le10000,q\le10000$

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.