結果
問題 | 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,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)