結果

問題 No.1365 [Cherry 1st Tune] Whose Fault?
ユーザー 👑 KazunKazun
提出日時 2021-01-17 03:02:21
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,451 bytes
コンパイル時間 432 ms
コンパイル使用メモリ 86,796 KB
実行使用メモリ 110,416 KB
最終ジャッジ日時 2023-08-28 11:15:53
合計ジャッジ時間 10,423 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,364 KB
testcase_01 AC 70 ms
71,596 KB
testcase_02 AC 72 ms
71,432 KB
testcase_03 AC 72 ms
71,572 KB
testcase_04 AC 72 ms
71,432 KB
testcase_05 AC 73 ms
71,408 KB
testcase_06 AC 74 ms
71,456 KB
testcase_07 AC 75 ms
71,584 KB
testcase_08 AC 74 ms
71,240 KB
testcase_09 AC 74 ms
71,436 KB
testcase_10 RE -
testcase_11 AC 73 ms
71,464 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 AC 175 ms
81,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#WA想定 (P_i=0 の考慮忘れ)

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
input=sys.stdin.readline
write=sys.stdout.write

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]
    U.set(i,[d,1/P[i]])

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