結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー GER_chenGER_chen
提出日時 2021-01-22 23:33:10
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,044 bytes
コンパイル時間 525 ms
コンパイル使用メモリ 86,300 KB
実行使用メモリ 119,852 KB
最終ジャッジ日時 2023-08-28 11:44:09
合計ジャッジ時間 11,937 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,292 KB
testcase_01 AC 93 ms
71,208 KB
testcase_02 AC 94 ms
70,948 KB
testcase_03 AC 135 ms
77,112 KB
testcase_04 AC 119 ms
77,036 KB
testcase_05 AC 163 ms
78,584 KB
testcase_06 AC 153 ms
78,828 KB
testcase_07 AC 160 ms
78,312 KB
testcase_08 AC 155 ms
77,936 KB
testcase_09 AC 104 ms
76,748 KB
testcase_10 RE -
testcase_11 AC 120 ms
77,628 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 AC 734 ms
78,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#yuki-1365
from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.roots = [-1]*n

    def find(self, x):  #根を見つける
        if self.roots[x] < 0:
            return x
        else:
            self.roots[x] = self.find(self.roots[x])  #つなぎ直す
            return self.roots[x]

    def union(self, x, y):  #x,yが入っている部分集合を合体させる
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.roots[x] > self.roots[y]:
            x, y = y, x
        self.roots[x] += self.roots[y]
        self.roots[y] = x

    def size(self, x):  #ある元が入っている部分集合の大きさを求める
        return -self.roots[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.roots) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):  #defaultdictの形で各部分集合を返す
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

N = int(input())
u = UnionFind(N)
A = list(map(int, input().split()))  #初期エネルギー
B = list(map(int, input().split()))  #目標エネルギー
P = list(map(int, input().split()))  #重みづけ
Q = int(input())
for _ in range(Q):
    X, Y = map(int, input().split())
    u.union(X-1, Y-1)
    fuan = 0
    for v in u.all_group_members().values():
        d, G = 0, []
        for i in v:
            d += A[i]-B[i]
            G.append(1/P[i])
        d = abs(d)
        g = sum(G)
        fuan += sum(x/(g**2) for x in G)*d**2
        
    print(fuan)
0