結果

問題 No.1293 2種類の道路
ユーザー 👑 tatyamtatyam
提出日時 2020-05-24 03:05:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 594 ms / 2,000 ms
コード長 1,374 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 114,352 KB
最終ジャッジ日時 2023-09-18 01:32:29
合計ジャッジ時間 9,233 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,296 KB
testcase_01 AC 73 ms
71,260 KB
testcase_02 AC 75 ms
71,328 KB
testcase_03 AC 77 ms
71,556 KB
testcase_04 AC 82 ms
71,428 KB
testcase_05 AC 78 ms
71,232 KB
testcase_06 AC 75 ms
71,548 KB
testcase_07 AC 73 ms
71,228 KB
testcase_08 AC 79 ms
71,536 KB
testcase_09 AC 586 ms
103,712 KB
testcase_10 AC 589 ms
103,632 KB
testcase_11 AC 575 ms
103,888 KB
testcase_12 AC 594 ms
103,828 KB
testcase_13 AC 593 ms
103,624 KB
testcase_14 AC 357 ms
101,924 KB
testcase_15 AC 367 ms
102,356 KB
testcase_16 AC 356 ms
104,248 KB
testcase_17 AC 355 ms
102,716 KB
testcase_18 AC 293 ms
97,944 KB
testcase_19 AC 358 ms
114,352 KB
testcase_20 AC 358 ms
113,764 KB
testcase_21 AC 305 ms
102,256 KB
testcase_22 AC 294 ms
101,696 KB
testcase_23 AC 292 ms
101,728 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