結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー chineristACchineristAC
提出日時 2021-01-22 22:37:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 729 ms / 2,000 ms
コード長 1,936 bytes
コンパイル時間 548 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 109,484 KB
最終ジャッジ日時 2023-08-28 11:27:32
合計ジャッジ時間 20,303 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,248 KB
testcase_01 AC 73 ms
71,280 KB
testcase_02 AC 73 ms
71,368 KB
testcase_03 AC 74 ms
71,324 KB
testcase_04 AC 74 ms
71,256 KB
testcase_05 AC 73 ms
71,324 KB
testcase_06 AC 74 ms
71,360 KB
testcase_07 AC 75 ms
71,296 KB
testcase_08 AC 75 ms
71,252 KB
testcase_09 AC 73 ms
71,452 KB
testcase_10 AC 73 ms
71,452 KB
testcase_11 AC 74 ms
71,512 KB
testcase_12 AC 74 ms
71,332 KB
testcase_13 AC 248 ms
81,640 KB
testcase_14 AC 233 ms
81,172 KB
testcase_15 AC 126 ms
78,240 KB
testcase_16 AC 90 ms
76,820 KB
testcase_17 AC 215 ms
80,248 KB
testcase_18 AC 158 ms
79,592 KB
testcase_19 AC 249 ms
81,824 KB
testcase_20 AC 150 ms
78,932 KB
testcase_21 AC 228 ms
80,588 KB
testcase_22 AC 214 ms
81,216 KB
testcase_23 AC 328 ms
94,496 KB
testcase_24 AC 488 ms
95,168 KB
testcase_25 AC 657 ms
102,748 KB
testcase_26 AC 531 ms
96,580 KB
testcase_27 AC 548 ms
95,000 KB
testcase_28 AC 387 ms
86,660 KB
testcase_29 AC 392 ms
86,588 KB
testcase_30 AC 729 ms
109,484 KB
testcase_31 AC 689 ms
107,296 KB
testcase_32 AC 519 ms
99,524 KB
testcase_33 AC 625 ms
100,180 KB
testcase_34 AC 543 ms
95,844 KB
testcase_35 AC 479 ms
95,204 KB
testcase_36 AC 630 ms
105,244 KB
testcase_37 AC 322 ms
83,772 KB
testcase_38 AC 274 ms
98,244 KB
testcase_39 AC 716 ms
107,456 KB
testcase_40 AC 694 ms
105,244 KB
testcase_41 AC 617 ms
101,332 KB
testcase_42 AC 554 ms
95,864 KB
testcase_43 AC 73 ms
71,388 KB
testcase_44 AC 216 ms
101,556 KB
testcase_45 AC 216 ms
91,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFindVerSize():
    def __init__(self, N,init_P,init_S):
        self._parent = [n for n in range(0, N)]
        self.P_sum = [0 for i in range(N)]
        for i in range(N):
            if init_P[i]:
                self.P_sum[i] = 1/init_P[i]
            else:
                self.P_sum[i] = float("inf")
        self.S_sum = [init_S[i] for i in range(N)]
        self._size = [1]*N
        self.group = N
        self.ans = 0
        for i in range(N):
            self.ans += (self.S_sum[i]**2)/self.P_sum[i]

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        stack = [x]
        while self._parent[stack[-1]]!=stack[-1]:
            stack.append(self._parent[stack[-1]])
        for v in stack:
            self._parent[v] = stack[-1]
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        self.group -= 1

        if self._size[gx] >= self._size[gy]:
            gx,gy = gy,gx

        self._parent[gx] = gy
        self._size[gy] += self._size[gx]

        self.ans -= self.S_sum[gx]**2 / self.P_sum[gx]
        self.ans -= self.S_sum[gy]**2 / self.P_sum[gy]

        self.S_sum[gy] += self.S_sum[gx]
        self.P_sum[gy] += self.P_sum[gx]

        self.ans += self.S_sum[gy]**2 / self.P_sum[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

import sys

input = sys.stdin.readline

N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
P = list(map(int,input().split()))

S = [A[i]-B[i] for i in range(N)]

uf = UnionFindVerSize(N,P,S)
ans = []
for _ in range(int(input())):
    x,y = map(int,input().split())
    uf.unite(x-1,y-1)
    ans.append(uf.ans)

print(*ans,sep="\n")
0