結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー convexineqconvexineq
提出日時 2021-01-23 14:03:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 983 ms / 2,000 ms
コード長 1,621 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 87,380 KB
実行使用メモリ 115,516 KB
最終ジャッジ日時 2023-08-29 00:45:30
合計ジャッジ時間 31,258 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,324 KB
testcase_01 AC 69 ms
71,480 KB
testcase_02 AC 67 ms
71,172 KB
testcase_03 AC 70 ms
71,756 KB
testcase_04 AC 75 ms
71,460 KB
testcase_05 AC 73 ms
71,584 KB
testcase_06 AC 74 ms
71,576 KB
testcase_07 AC 74 ms
71,544 KB
testcase_08 AC 73 ms
71,436 KB
testcase_09 AC 70 ms
71,340 KB
testcase_10 AC 70 ms
71,396 KB
testcase_11 AC 69 ms
71,484 KB
testcase_12 AC 72 ms
71,468 KB
testcase_13 AC 256 ms
80,264 KB
testcase_14 AC 232 ms
80,240 KB
testcase_15 AC 138 ms
78,332 KB
testcase_16 AC 97 ms
77,912 KB
testcase_17 AC 219 ms
79,424 KB
testcase_18 AC 187 ms
79,340 KB
testcase_19 AC 272 ms
81,220 KB
testcase_20 AC 198 ms
78,104 KB
testcase_21 AC 250 ms
80,240 KB
testcase_22 AC 232 ms
79,956 KB
testcase_23 AC 364 ms
94,204 KB
testcase_24 AC 534 ms
94,268 KB
testcase_25 AC 865 ms
97,332 KB
testcase_26 AC 639 ms
95,056 KB
testcase_27 AC 735 ms
87,108 KB
testcase_28 AC 471 ms
81,288 KB
testcase_29 AC 387 ms
85,288 KB
testcase_30 AC 983 ms
99,180 KB
testcase_31 AC 889 ms
106,632 KB
testcase_32 AC 592 ms
99,196 KB
testcase_33 AC 807 ms
94,124 KB
testcase_34 AC 688 ms
91,832 KB
testcase_35 AC 510 ms
94,400 KB
testcase_36 AC 787 ms
102,256 KB
testcase_37 AC 338 ms
84,612 KB
testcase_38 AC 339 ms
96,280 KB
testcase_39 AC 922 ms
94,084 KB
testcase_40 AC 933 ms
94,088 KB
testcase_41 AC 733 ms
99,048 KB
testcase_42 AC 753 ms
85,200 KB
testcase_43 AC 67 ms
71,648 KB
testcase_44 AC 388 ms
115,516 KB
testcase_45 AC 544 ms
78,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]

n = int(input())
*a, = map(int,input().split())
*b, = map(int,input().split())
*p, = map(float,input().split())

d = [ai-bi for ai,bi in zip(a,b)]
UF = UnionFind(n)
q = int(input())
ans = sum(pi*di**2 for pi,di in zip(p,d))
eps = 1e-9
for _ in range(q):
    x,y = map(int,input().split())
    x -= 1; y -= 1
    rx,ry = UF.root(x), UF.root(y)
    if rx != ry:
        ans -= p[rx]*d[rx]**2 + p[ry]*d[ry]**2
        UF.merge(rx,ry)
        nr = UF.root(rx)
        if abs(p[rx]) < eps or abs(p[ry]) < eps:
            p[nr] = 0.0
        else:
            p[nr] = 1/(1/p[rx]+1/p[ry])
        d[nr] = d[rx]+d[ry]
        ans += p[nr]*d[nr]**2
    print(ans)
0