結果

問題 No.1293 2種類の道路
ユーザー rlangevinrlangevin
提出日時 2024-02-02 12:04:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 504 ms / 2,000 ms
コード長 1,510 bytes
コンパイル時間 472 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 106,024 KB
最終ジャッジ日時 2024-02-02 12:05:07
合計ジャッジ時間 7,926 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 36 ms
53,460 KB
testcase_06 AC 36 ms
53,460 KB
testcase_07 AC 38 ms
53,460 KB
testcase_08 AC 37 ms
53,460 KB
testcase_09 AC 485 ms
93,708 KB
testcase_10 AC 504 ms
93,416 KB
testcase_11 AC 469 ms
93,748 KB
testcase_12 AC 470 ms
92,264 KB
testcase_13 AC 480 ms
93,416 KB
testcase_14 AC 308 ms
103,912 KB
testcase_15 AC 304 ms
103,144 KB
testcase_16 AC 220 ms
94,296 KB
testcase_17 AC 210 ms
97,240 KB
testcase_18 AC 191 ms
100,172 KB
testcase_19 AC 294 ms
106,024 KB
testcase_20 AC 296 ms
105,960 KB
testcase_21 AC 122 ms
75,884 KB
testcase_22 AC 124 ms
76,016 KB
testcase_23 AC 113 ms
75,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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):
        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 = 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):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]
    
    
N, D, W = map(int, input().split())
Ux, Uy = UnionFind(N), UnionFind(N)
for i in range(D):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    Ux.union(a, b)
for i in range(W):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    Uy.union(a, b)
    
S = set()
mem = [[] for i in range(N)]
for i in range(N):
    mem[Ux.find(i)].append(i)
    
ans = 0
for i in range(N):
    uxi = Ux.find(i)
    if uxi in S:
        continue
    S.add(uxi)
    temp = set()
    for u in mem[uxi]:
        temp.add(Uy.find(u))
    cnt = -1
    for u in temp:
        cnt += Uy.get_size(u)
    ans += len(mem[uxi]) * cnt
    
print(ans)
0