結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー 👑 KazunKazun
提出日時 2021-01-17 02:36:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 791 ms / 2,000 ms
コード長 3,536 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 86,044 KB
実行使用メモリ 143,416 KB
最終ジャッジ日時 2023-08-28 11:15:21
合計ジャッジ時間 25,748 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 222 ms
89,152 KB
testcase_01 AC 220 ms
89,148 KB
testcase_02 AC 220 ms
89,168 KB
testcase_03 AC 221 ms
89,152 KB
testcase_04 AC 224 ms
89,160 KB
testcase_05 AC 223 ms
89,092 KB
testcase_06 AC 222 ms
89,168 KB
testcase_07 AC 229 ms
91,236 KB
testcase_08 AC 226 ms
89,132 KB
testcase_09 AC 226 ms
89,024 KB
testcase_10 AC 221 ms
89,188 KB
testcase_11 AC 219 ms
89,068 KB
testcase_12 AC 221 ms
89,204 KB
testcase_13 AC 372 ms
96,960 KB
testcase_14 AC 356 ms
97,908 KB
testcase_15 AC 279 ms
94,340 KB
testcase_16 AC 249 ms
92,868 KB
testcase_17 AC 351 ms
96,464 KB
testcase_18 AC 343 ms
98,536 KB
testcase_19 AC 371 ms
97,948 KB
testcase_20 AC 315 ms
95,392 KB
testcase_21 AC 355 ms
96,672 KB
testcase_22 AC 349 ms
96,820 KB
testcase_23 AC 462 ms
119,324 KB
testcase_24 AC 578 ms
125,200 KB
testcase_25 AC 719 ms
129,168 KB
testcase_26 AC 630 ms
125,616 KB
testcase_27 AC 631 ms
113,136 KB
testcase_28 AC 472 ms
103,024 KB
testcase_29 AC 465 ms
106,128 KB
testcase_30 AC 791 ms
132,788 KB
testcase_31 AC 775 ms
138,312 KB
testcase_32 AC 607 ms
133,440 KB
testcase_33 AC 685 ms
123,224 KB
testcase_34 AC 627 ms
118,220 KB
testcase_35 AC 547 ms
124,176 KB
testcase_36 AC 703 ms
137,604 KB
testcase_37 AC 432 ms
104,084 KB
testcase_38 AC 457 ms
129,352 KB
testcase_39 AC 769 ms
126,496 KB
testcase_40 AC 739 ms
126,236 KB
testcase_41 AC 691 ms
132,976 KB
testcase_42 AC 621 ms
112,488 KB
testcase_43 AC 222 ms
89,072 KB
testcase_44 AC 438 ms
143,416 KB
testcase_45 AC 323 ms
96,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Coloring_Union_Find():
    def __init__(self,N,f,e=0):
        """0,1,...,n-1を要素として初期化する.

        N:要素数
        f:2変数関数の合成
        e:最初の値
        """
        self.n=N
        self.parents=[-1]*N
        self.data=[e]*N
        self.rank=[0]*N
        self.f=f

    def find(self, x):
        """要素xの属している族を調べる.

        x:要素
        """
        V=[]
        while self.parents[x]>=0:
            V.append(x)
            x=self.parents[x]

        for v in V:
            self.parents[v]=x
        return x

    def union(self, x, y):
        """要素x,yを同一視する.

        x,y:要素
        """
        x=self.find(x)
        y=self.find(y)

        if x==y:
            return

        self.data[x]=self.data[y]=self.f(self.data[x],self.data[y])

        if self.rank[x]<self.rank[y]:
            x,y=y,x

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

        if self.rank[x]==self.rank[y]:
            self.rank[x]+=1

    def size(self, x):
        """要素xの属している要素の数.

        x:要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """要素x,yは同一視されているか?

        x,y:要素
        """
        return self.find(x) == self.find(y)

    def set(self,x,c):
        """要素xの属する成分の色をcに変更する.

        x:要素
        c:色
        """
        self.data[self.find(x)]=c
        return

    def look(self,x):
        """要素xの属する成分の色

        x:要素
        """
        return self.data[self.find(x)]

    def members(self, x):
        """要素xが属している族の要素.
        ※族の要素の個数が欲しいときはsizeを使うこと!!

        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):
        """全ての族の出力
        """
        X={r:[] for r in self.roots()}
        for k in range(self.n):
            X[self.find(k)].append(k)
        return X

    def color_list(self):
        return [self.look(x) for x in range(self.n)]

    def color_map(self):
        return {x:self.look(x) for x in self.roots()}

    def __str__(self):
        return '\n'.join('{} [color:{}]: {}'.format(r,self.look(r),self.members(r)) for r in self.roots())

    def __repr__(self):
        return self.__str__()
#================================================
import sys,decimal
from decimal import Decimal
input=sys.stdin.readline
write=sys.stdout.write
decimal.getcontext().prec = 30

N=int(input())
A=["*"]+list(map(int,input().split()))
B=["*"]+list(map(int,input().split()))
P=["*"]+list(map(int,input().split()))

inf=float("inf")
U=Coloring_Union_Find(N+1,lambda x,y:[x[0]+y[0],x[1]+y[1]],[0,0])

Ans=0
for i in range(1,N+1):
    d=A[i]-B[i]
    Ans+=d*d*P[i]
    if P[i]:
        U.set(i,[d,1/P[i]])
    else:
        U.set(i,[d,inf])


Q=int(input())
S=[0]*Q
for q in range(Q):
    X,Y=map(int,input().split())

    if not U.same(X,Y):
        d,p=U.look(X)
        Ans-=d*d/p
        d,p=U.look(Y)
        Ans-=d*d/p

        U.union(X,Y)
        d,p=U.look(X)
        Ans+=d*d/p
    S[q]=Ans

write("\n".join(map(str,S)))
0