結果
問題 | No.1365 [Cherry 1st Tune] Whose Fault? |
ユーザー |
![]() |
提出日時 | 2021-01-22 22:59:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 838 ms / 2,000 ms |
コード長 | 2,106 bytes |
コンパイル時間 | 346 ms |
コンパイル使用メモリ | 82,496 KB |
実行使用メモリ | 117,248 KB |
最終ジャッジ日時 | 2024-12-29 09:52:34 |
合計ジャッジ時間 | 19,823 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 46 |
ソースコード
import os,ioinput=io.BytesIO(os.read(0,os.fstat(0).st_size)).readlinen=int(input())class UnionFind():def __init__(self, n):self.n = nself.parents = [-1] * ndef find(self, x):if self.parents[x] < 0:return xelse: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:returnif self.parents[x] > self.parents[y]:x, y = y, xself.parents[x] += self.parents[y]self.parents[y] = xdef 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=0flag0=[0]*nsump=[0]*ndifs=[0]*nfor i in range(n):if p[i]:sump[i]=1/p[i]else:flag0[i]=1for i in range(n):cost+=p[i]*(a[i]-b[i])**2difs[i]=(a[i]-b[i])for _ in range(q):u,v=map(int,input().split())if uf.same(u-1,v-1):print(cost)continueroot1=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]=tmpsump[root2]=tmptmp=difs[root1]+difs[root2]difs[root1]=tmpdifs[root2]=tmpif flag0[root1] or flag0[root2]:flag0[root1]=1flag0[root2]=1if not flag0[root1]:cost+=difs[root1]**2/sump[root1]print(cost)