結果

問題 No.1293 2種類の道路
ユーザー 👑 KazunKazun
提出日時 2020-11-21 01:42:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 2,503 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 111,308 KB
最終ジャッジ日時 2023-09-30 20:27:51
合計ジャッジ時間 6,951 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,224 KB
testcase_01 AC 77 ms
71,224 KB
testcase_02 AC 77 ms
71,112 KB
testcase_03 AC 78 ms
71,228 KB
testcase_04 AC 78 ms
71,408 KB
testcase_05 AC 79 ms
71,416 KB
testcase_06 AC 78 ms
71,296 KB
testcase_07 AC 77 ms
71,296 KB
testcase_08 AC 81 ms
71,152 KB
testcase_09 AC 384 ms
87,176 KB
testcase_10 AC 367 ms
86,756 KB
testcase_11 AC 378 ms
86,556 KB
testcase_12 AC 372 ms
86,940 KB
testcase_13 AC 372 ms
87,388 KB
testcase_14 AC 259 ms
96,368 KB
testcase_15 AC 252 ms
96,752 KB
testcase_16 AC 300 ms
89,460 KB
testcase_17 AC 301 ms
90,588 KB
testcase_18 AC 253 ms
95,444 KB
testcase_19 AC 278 ms
111,196 KB
testcase_20 AC 281 ms
111,308 KB
testcase_21 AC 216 ms
77,908 KB
testcase_22 AC 208 ms
77,684 KB
testcase_23 AC 206 ms
77,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

        N:要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.rank=[0]*N

    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

        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 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 __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
#================================================
import sys
input=sys.stdin.readline
N,D,W=map(int,input().split())
U=Union_Find(N+1)
for _ in range(D):
    a,b=map(int,input().split())
    U.union(a,b)

V=Union_Find(N+1)
for j in range(W):
    c,d=map(int,input().split())
    V.union(c,d)

G=U.all_group_members()
K=0
for t in G:
    if t==0:continue

    M=set()
    X=0
    for a in G[t]:
        f=V.find(a)
        if f in M:
            continue

        M.add(f)
        X+=V.size(a)
    K+=X*len(G[t])
print(K-N)
0