結果

問題 No.1293 2種類の道路
ユーザー 👑 tatyamtatyam
提出日時 2020-05-24 03:00:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,161 ms / 2,000 ms
コード長 1,374 bytes
コンパイル時間 486 ms
コンパイル使用メモリ 10,932 KB
実行使用メモリ 64,832 KB
最終ジャッジ日時 2023-09-18 01:32:18
合計ジャッジ時間 16,044 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,016 KB
testcase_01 AC 18 ms
8,168 KB
testcase_02 AC 16 ms
8,124 KB
testcase_03 AC 16 ms
8,084 KB
testcase_04 AC 18 ms
8,064 KB
testcase_05 AC 17 ms
8,100 KB
testcase_06 AC 17 ms
8,064 KB
testcase_07 AC 17 ms
8,156 KB
testcase_08 AC 18 ms
7,964 KB
testcase_09 AC 1,152 ms
64,780 KB
testcase_10 AC 1,156 ms
64,832 KB
testcase_11 AC 1,161 ms
64,820 KB
testcase_12 AC 1,134 ms
64,760 KB
testcase_13 AC 1,155 ms
64,648 KB
testcase_14 AC 754 ms
56,920 KB
testcase_15 AC 745 ms
56,872 KB
testcase_16 AC 1,066 ms
59,596 KB
testcase_17 AC 1,061 ms
62,984 KB
testcase_18 AC 709 ms
52,376 KB
testcase_19 AC 744 ms
49,884 KB
testcase_20 AC 743 ms
49,844 KB
testcase_21 AC 763 ms
38,500 KB
testcase_22 AC 747 ms
38,620 KB
testcase_23 AC 736 ms
39,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, size):
        self.data = [-1] * size
    def root(self, x):
        if self.data[x] < 0:
            return x
        ans = self.root(self.data[x])
        self.data[x] = ans
        return ans
    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False
        if self.data[x] > self.data[y]:
            x, y = y, x
        self.data[x] += self.data[y]
        self.data[y] = x
        return True
    def size(self, x):
        return -self.data[self.root(x)]

n, d, w = map(int, input().split())
assert(2 <= n <= 100000)
assert(1 <= d <= min(100000, n * (n + 1) // 2))
assert(1 <= w <= min(100000, n * (n + 1) // 2))

a = []
for i in range(d):
    x, y = map(int, input().split())
    assert(1 <= x < y <= n)
    a.append((x - 1, y - 1))
assert(len(set(a)) == d)
b = []
for i in range(w):
    x, y = map(int, input().split())
    assert(1 <= x < y <= n)
    b.append((x - 1, y - 1))
assert(len(set(b)) == w)

car = UnionFind(n)
walk = UnionFind(n)
for x, y in a:
    car.unite(x, y)
for x, y in b:
    walk.unite(x, y)

cnt = [0] * n
s = [set() for i in range(n)]
for i in range(n):
    x = car.root(i)
    y = walk.root(i)
    if y not in s[x]:
        s[x].add(y)
        cnt[x] += walk.size(y)

ans = 0
for i in range(n):
    ans += cnt[car.root(i)] - 1
print(ans)
0