結果
問題 | No.1365 [Cherry 1st Tune] Whose Fault? |
ユーザー |
![]() |
提出日時 | 2021-01-23 14:03:36 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,158 ms / 2,000 ms |
コード長 | 1,621 bytes |
コンパイル時間 | 495 ms |
コンパイル使用メモリ | 82,688 KB |
実行使用メモリ | 104,704 KB |
最終ジャッジ日時 | 2024-12-30 17:52:04 |
合計ジャッジ時間 | 34,304 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 46 |
ソースコード
class UnionFind:def __init__(self, n):self.parent = list(range(n)) #親ノードself.size = [1]*n #グループの要素数def root(self, x): #root(x): xの根ノードを返す.while self.parent[x] != x:self.parent[x] = self.parent[self.parent[x]]x = self.parent[x]return xdef merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめるx, y = self.root(x), self.root(y)if x == y: return Falseif self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいようにself.size[x] += self.size[y] #xの要素数を更新self.parent[y] = x #yをxにつなぐreturn Truedef issame(self, x, y): #same(x,y): xとyが同じ組ならTruereturn self.root(x) == self.root(y)def getsize(self,x): #size(x): xのいるグループの要素数を返すreturn self.size[self.root(x)]n = int(input())*a, = map(int,input().split())*b, = map(int,input().split())*p, = map(float,input().split())d = [ai-bi for ai,bi in zip(a,b)]UF = UnionFind(n)q = int(input())ans = sum(pi*di**2 for pi,di in zip(p,d))eps = 1e-9for _ in range(q):x,y = map(int,input().split())x -= 1; y -= 1rx,ry = UF.root(x), UF.root(y)if rx != ry:ans -= p[rx]*d[rx]**2 + p[ry]*d[ry]**2UF.merge(rx,ry)nr = UF.root(rx)if abs(p[rx]) < eps or abs(p[ry]) < eps:p[nr] = 0.0else:p[nr] = 1/(1/p[rx]+1/p[ry])d[nr] = d[rx]+d[ry]ans += p[nr]*d[nr]**2print(ans)