結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー GER_chenGER_chen
提出日時 2021-01-23 13:43:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,272 ms / 2,000 ms
コード長 2,283 bytes
コンパイル時間 289 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 119,164 KB
最終ジャッジ日時 2023-08-29 00:26:29
合計ジャッジ時間 37,798 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
71,688 KB
testcase_01 AC 87 ms
71,484 KB
testcase_02 AC 87 ms
71,744 KB
testcase_03 AC 90 ms
72,284 KB
testcase_04 AC 90 ms
72,348 KB
testcase_05 AC 91 ms
72,332 KB
testcase_06 AC 95 ms
72,332 KB
testcase_07 AC 95 ms
72,232 KB
testcase_08 AC 93 ms
72,564 KB
testcase_09 AC 91 ms
71,748 KB
testcase_10 AC 87 ms
71,480 KB
testcase_11 AC 89 ms
71,720 KB
testcase_12 AC 95 ms
72,584 KB
testcase_13 AC 328 ms
82,804 KB
testcase_14 AC 338 ms
84,000 KB
testcase_15 AC 180 ms
80,468 KB
testcase_16 AC 124 ms
78,272 KB
testcase_17 AC 297 ms
82,756 KB
testcase_18 AC 232 ms
82,180 KB
testcase_19 AC 360 ms
84,232 KB
testcase_20 AC 233 ms
80,280 KB
testcase_21 AC 321 ms
81,884 KB
testcase_22 AC 292 ms
81,836 KB
testcase_23 AC 487 ms
101,028 KB
testcase_24 AC 771 ms
104,524 KB
testcase_25 AC 1,109 ms
108,956 KB
testcase_26 AC 880 ms
105,040 KB
testcase_27 AC 961 ms
90,104 KB
testcase_28 AC 567 ms
83,784 KB
testcase_29 AC 529 ms
88,860 KB
testcase_30 AC 1,272 ms
111,688 KB
testcase_31 AC 1,178 ms
114,876 KB
testcase_32 AC 833 ms
111,712 KB
testcase_33 AC 1,108 ms
100,692 KB
testcase_34 AC 940 ms
97,452 KB
testcase_35 AC 738 ms
103,028 KB
testcase_36 AC 1,088 ms
115,080 KB
testcase_37 AC 462 ms
87,060 KB
testcase_38 AC 413 ms
108,456 KB
testcase_39 AC 1,239 ms
104,496 KB
testcase_40 AC 1,212 ms
104,508 KB
testcase_41 AC 1,040 ms
111,756 KB
testcase_42 AC 958 ms
89,212 KB
testcase_43 AC 85 ms
71,684 KB
testcase_44 AC 441 ms
119,164 KB
testcase_45 AC 524 ms
77,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#yuki-1365
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))  #初期エネルギー
B = list(map(int, input().split()))  #目標エネルギー
P = list(map(int, input().split()))  #重みづけ
P2 = [float('inf') if p == 0 else 1/p for p in P]

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.roots = [[-1, A[i], B[i], P2[i]] for i in range(n)]
        self.cost = sum((A[i]-B[i])**2/P2[i] for i in range(n))

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

    def union(self, x, y):  #x,yが入っている部分集合を合体させる
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return self.cost
        if self.roots[x][0] > self.roots[y][0]:
            x, y = y, x
        vx, vy = self.roots[x], self.roots[y]
        self.cost -= (vx[1]-vx[2])**2/vx[3]+(vy[1]-vy[2])**2/vy[3]
        for i in range(4):
            self.roots[x][i] += self.roots[y][i]
        self.roots[y][0] = x
        vx = self.roots[x]
        self.cost += (vx[1]-vx[2])**2/vx[3]
        return self.cost

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

    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 rootss(self):
        return [i for i, x in enumerate(self.roots) if x[0] < 0]

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

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

u = UnionFind(N)

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())
    print(u.union(X-1, Y-1))
0