結果

問題 No.1293 2種類の道路
ユーザー ygd.ygd.
提出日時 2020-11-21 23:14:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 615 ms / 2,000 ms
コード長 1,498 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 98,992 KB
最終ジャッジ日時 2023-09-30 22:19:55
合計ジャッジ時間 8,481 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,160 KB
testcase_01 AC 78 ms
71,324 KB
testcase_02 AC 77 ms
71,004 KB
testcase_03 AC 77 ms
71,488 KB
testcase_04 AC 78 ms
71,048 KB
testcase_05 AC 76 ms
70,992 KB
testcase_06 AC 77 ms
71,164 KB
testcase_07 AC 76 ms
71,352 KB
testcase_08 AC 79 ms
71,104 KB
testcase_09 AC 605 ms
91,656 KB
testcase_10 AC 615 ms
90,824 KB
testcase_11 AC 603 ms
90,864 KB
testcase_12 AC 603 ms
91,304 KB
testcase_13 AC 603 ms
90,300 KB
testcase_14 AC 372 ms
98,876 KB
testcase_15 AC 368 ms
98,992 KB
testcase_16 AC 326 ms
92,628 KB
testcase_17 AC 329 ms
90,956 KB
testcase_18 AC 271 ms
96,208 KB
testcase_19 AC 376 ms
98,932 KB
testcase_20 AC 359 ms
98,600 KB
testcase_21 AC 234 ms
78,144 KB
testcase_22 AC 240 ms
78,468 KB
testcase_23 AC 224 ms
78,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]
    def find(self, x):
        """
        x が属するグループを探索して親を出す。
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]
    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)
    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]
N,D,W = map(int,input().split())
V = UnionFind(N)
F = UnionFind(N) 
for i in range(D):
  a,b = map(int,input().split())
  a-=1;b-=1
  V.union(a,b)
for i in range(W):
  a,b = map(int,input().split())
  a-=1;b-=1
  F.union(a,b)
ans = 0
S = set([])
for i in range(N):
  temp = (V.find(i),F.find(i))
  S.add(temp)
for x,y in S:
  temp = V.get_size(x)*F.get_size(y)
  ans += temp
print(ans-N)
0