結果

問題 No.1293 2種類の道路
ユーザー roarisroaris
提出日時 2020-11-22 20:43:17
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 387 ms / 2,000 ms
コード長 1,475 bytes
コンパイル時間 484 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 126,412 KB
最終ジャッジ日時 2023-09-30 23:04:07
合計ジャッジ時間 7,287 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,552 KB
testcase_01 AC 93 ms
71,556 KB
testcase_02 AC 93 ms
71,584 KB
testcase_03 AC 93 ms
71,484 KB
testcase_04 AC 95 ms
71,572 KB
testcase_05 AC 93 ms
71,496 KB
testcase_06 AC 93 ms
71,500 KB
testcase_07 AC 94 ms
71,724 KB
testcase_08 AC 94 ms
71,436 KB
testcase_09 AC 387 ms
91,204 KB
testcase_10 AC 382 ms
89,964 KB
testcase_11 AC 386 ms
89,980 KB
testcase_12 AC 381 ms
90,904 KB
testcase_13 AC 377 ms
90,680 KB
testcase_14 AC 275 ms
92,952 KB
testcase_15 AC 276 ms
92,956 KB
testcase_16 AC 307 ms
99,584 KB
testcase_17 AC 305 ms
100,428 KB
testcase_18 AC 267 ms
112,580 KB
testcase_19 AC 301 ms
126,412 KB
testcase_20 AC 302 ms
126,168 KB
testcase_21 AC 219 ms
78,000 KB
testcase_22 AC 226 ms
78,256 KB
testcase_23 AC 219 ms
77,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

class Unionfind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [1]*n
    
    def root(self, x):
        r = x
        
        while not self.par[r]<0:
            r = self.par[r]
        
        t = x
        
        while t!=r:
            tmp = t
            t = self.par[t]
            self.par[tmp] = r
        
        return r
    
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        
        if rx==ry:
            return
        
        if self.rank[rx]<=self.rank[ry]:
            self.par[ry] += self.par[rx]
            self.par[rx] = ry
            
            if self.rank[rx]==self.rank[ry]:
                self.rank[ry] += 1
        else:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]

N, D, W = map(int, input().split())
uf1 = Unionfind(N)
uf2 = Unionfind(N)

for _ in range(D):
    a, b = map(int, input().split())
    uf1.unite(a-1, b-1)

for _ in range(W):
    c, d = map(int, input().split())
    uf2.unite(c-1, d-1)

d = defaultdict(set)

for i in range(N):
    d[uf1.root(i)].add(uf2.root(i))

cnt = defaultdict(int)

for k in d:
    for v in d[k]:
        cnt[k] += uf2.count(v)

ans = 0

for i in range(N):
    ans += cnt[uf1.root(i)]-1

print(ans)
0