結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー GER_chenGER_chen
提出日時 2021-01-23 00:18:36
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,277 bytes
コンパイル時間 1,282 ms
コンパイル使用メモリ 86,176 KB
実行使用メモリ 99,684 KB
最終ジャッジ日時 2023-08-28 11:56:12
合計ジャッジ時間 17,701 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
75,552 KB
testcase_01 AC 93 ms
71,240 KB
testcase_02 AC 93 ms
71,440 KB
testcase_03 AC 110 ms
76,584 KB
testcase_04 AC 105 ms
77,032 KB
testcase_05 AC 109 ms
76,860 KB
testcase_06 AC 109 ms
77,012 KB
testcase_07 AC 115 ms
77,312 KB
testcase_08 AC 111 ms
77,108 KB
testcase_09 AC 95 ms
71,104 KB
testcase_10 AC 95 ms
71,524 KB
testcase_11 AC 106 ms
76,864 KB
testcase_12 AC 109 ms
77,124 KB
testcase_13 AC 1,465 ms
86,508 KB
testcase_14 TLE -
testcase_15 AC 303 ms
78,932 KB
testcase_16 AC 161 ms
78,032 KB
testcase_17 AC 946 ms
86,352 KB
testcase_18 AC 775 ms
83,268 KB
testcase_19 AC 1,831 ms
88,324 KB
testcase_20 AC 292 ms
79,188 KB
testcase_21 AC 1,276 ms
85,000 KB
testcase_22 AC 829 ms
81,988 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
権限があれば一括ダウンロードができます

ソースコード

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())

def cost(v):
    d, g = 0, 0
    for i in v:
        d += A[i]-B[i]
        if P[i] != 0:
            g += 1/P[i]
        else:
            return 0, float('inf'), 0
    return d, g, d**2/g
    
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())
ans = sum(P[i]*(A[i]-B[i])**2 for i in range(N))
for _ in range(Q):
    X, Y = map(int, input().split())
    if u.same(X-1, Y-1):
        pass
    else:
        vX, vY = u.members(X-1), u.members(Y-1)
        dX, gX, cX = cost(vX)
        dY, gY, cY = cost(vY)
        d = dX+dY
        g = gX+gY
        c = d**2/g
        ans += c-cX-cY
        u.union(X-1, Y-1)
    print(ans)
0