結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー yuusanlondonyuusanlondon
提出日時 2021-01-22 22:59:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 793 ms / 2,000 ms
コード長 2,106 bytes
コンパイル時間 844 ms
コンパイル使用メモリ 87,036 KB
実行使用メモリ 113,212 KB
最終ジャッジ日時 2023-08-28 11:36:44
合計ジャッジ時間 21,030 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,232 KB
testcase_01 AC 72 ms
70,956 KB
testcase_02 AC 72 ms
71,276 KB
testcase_03 AC 72 ms
71,276 KB
testcase_04 AC 74 ms
71,140 KB
testcase_05 AC 73 ms
71,260 KB
testcase_06 AC 75 ms
71,320 KB
testcase_07 AC 75 ms
71,300 KB
testcase_08 AC 73 ms
71,308 KB
testcase_09 AC 75 ms
70,956 KB
testcase_10 AC 74 ms
71,136 KB
testcase_11 AC 73 ms
71,380 KB
testcase_12 AC 74 ms
71,232 KB
testcase_13 AC 290 ms
80,964 KB
testcase_14 AC 291 ms
81,764 KB
testcase_15 AC 118 ms
78,208 KB
testcase_16 AC 89 ms
76,404 KB
testcase_17 AC 249 ms
80,492 KB
testcase_18 AC 178 ms
79,084 KB
testcase_19 AC 284 ms
81,808 KB
testcase_20 AC 165 ms
78,924 KB
testcase_21 AC 286 ms
81,432 KB
testcase_22 AC 267 ms
82,164 KB
testcase_23 AC 385 ms
94,952 KB
testcase_24 AC 647 ms
102,012 KB
testcase_25 AC 741 ms
108,100 KB
testcase_26 AC 661 ms
102,420 KB
testcase_27 AC 594 ms
88,116 KB
testcase_28 AC 410 ms
83,716 KB
testcase_29 AC 444 ms
85,216 KB
testcase_30 AC 789 ms
113,212 KB
testcase_31 AC 781 ms
112,092 KB
testcase_32 AC 655 ms
110,632 KB
testcase_33 AC 678 ms
97,688 KB
testcase_34 AC 596 ms
93,812 KB
testcase_35 AC 589 ms
101,556 KB
testcase_36 AC 743 ms
110,816 KB
testcase_37 AC 458 ms
84,728 KB
testcase_38 AC 312 ms
105,408 KB
testcase_39 AC 748 ms
104,760 KB
testcase_40 AC 793 ms
104,608 KB
testcase_41 AC 730 ms
112,080 KB
testcase_42 AC 600 ms
87,668 KB
testcase_43 AC 72 ms
71,252 KB
testcase_44 AC 186 ms
111,964 KB
testcase_45 AC 147 ms
78,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n=int(input())
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[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.parents) if x < 0]

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

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
uf=UnionFind(n)
a=list(map(int,input().split()))
b=list(map(int,input().split()))
p=list(map(int,input().split()))
q=int(input())
cost=0
flag0=[0]*n
sump=[0]*n
difs=[0]*n
for i in range(n):
  if p[i]:
    sump[i]=1/p[i]
  else:
    flag0[i]=1
for i in range(n):
  cost+=p[i]*(a[i]-b[i])**2
  difs[i]=(a[i]-b[i])
for _ in range(q):
  u,v=map(int,input().split())
  if uf.same(u-1,v-1):
    print(cost)
    continue
  root1=uf.find(u-1)
  root2=uf.find(v-1)
  if not flag0[root1]:
    cost-=difs[root1]**2/sump[root1]
  if not flag0[root2]:
    cost-=difs[root2]**2/sump[root2]
  uf.union(u-1,v-1)
  tmp=sump[root1]+sump[root2]
  sump[root1]=tmp
  sump[root2]=tmp
  tmp=difs[root1]+difs[root2]
  difs[root1]=tmp
  difs[root2]=tmp
  if flag0[root1] or flag0[root2]:
    flag0[root1]=1
    flag0[root2]=1
  if not flag0[root1]:
    cost+=difs[root1]**2/sump[root1]
  print(cost)
0