結果
| 問題 |
No.1293 2種類の道路
|
| コンテスト | |
| ユーザー |
brthyyjp
|
| 提出日時 | 2021-09-15 10:08:03 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 453 ms / 2,000 ms |
| コード長 | 1,525 bytes |
| コンパイル時間 | 717 ms |
| コンパイル使用メモリ | 81,468 KB |
| 実行使用メモリ | 116,792 KB |
| 最終ジャッジ日時 | 2024-06-28 09:24:52 |
| 合計ジャッジ時間 | 7,738 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
ソースコード
class UnionFind:
def __init__(self, n):
self.par = [-1]*n
self.rank = [0]*n
def Find(self, x):
if self.par[x] < 0:
return x
else:
self.par[x] = self.Find(self.par[x])
return self.par[x]
def Unite(self, x, y):
x = self.Find(x)
y = self.Find(y)
if x != y:
if self.rank[x] < self.rank[y]:
self.par[y] += self.par[x]
self.par[x] = y
else:
self.par[x] += self.par[y]
self.par[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
def Same(self, x, y):
return self.Find(x) == self.Find(y)
def Size(self, x):
return -self.par[self.Find(x)]
import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from collections import defaultdict
n, d, w = map(int, input().split())
uf1 = UnionFind(n)
for i in range(d):
a, b = map(int, input().split())
a, b = a-1, b-1
uf1.Unite(a, b)
uf2 = UnionFind(n)
for i in range(w):
c, d = map(int, input().split())
c, d = c-1, d-1
uf2.Unite(c, d)
X = []
for i in range(n):
if uf1.par[i] < 0:
X.append(i)
CX = defaultdict(lambda: 0)
usedY = defaultdict(lambda: set())
for i in range(n):
x = uf1.Find(i)
y = uf2.Find(i)
if y in usedY[x]:
continue
CX[x] += uf2.Size(y)
usedY[x].add(y)
ans = 0
for x in X:
ans += uf1.Size(x)*(CX[x]-1)
print(ans)
brthyyjp