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_b386fbff938636daf0cb3833934508e2.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_4b56ee4a4f26a11149cc826b59c4f860.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|\dfrac{\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.