結果
問題 | No.1293 2種類の道路 |
ユーザー | Navier_Boltzmann |
提出日時 | 2023-09-13 12:34:25 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 748 ms / 2,000 ms |
コード長 | 1,931 bytes |
コンパイル時間 | 642 ms |
コンパイル使用メモリ | 81,784 KB |
実行使用メモリ | 119,376 KB |
最終ジャッジ日時 | 2024-06-30 21:34:59 |
合計ジャッジ時間 | 9,740 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
from collections import * from itertools import * from functools import * from heapq import * import sys,math input = sys.stdin.readline class DSU: def __init__(self, n): self._n = n self.parent_or_size = [-1] * n self.member = [[i] for i in range(n)] def merge(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n x, y = self.leader(a), self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] for tmp in self.member[y]: self.member[x].append(tmp) self.parent_or_size[y] = x return x def members(self,a): return self.member[self.leader(a)] def same(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a): assert 0 <= a < self._n if self.parent_or_size[a] < 0: return a self.parent_or_size[a] = self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self, a): assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf = [self.leader(i) for i in range(self._n)] result = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return [r for r in result if r != []] N,D,W = map(int,input().split()) T = DSU(N) for _ in range(D): a,b = map(int,input().split()) a -= 1 b -= 1 T.merge(a,b) S = DSU(N) for _ in range(W): c,d = map(int,input().split()) c -= 1 d -= 1 S.merge(c,d) ans = 0 for gt in T.groups(): tt = set() for t in gt: tt.add(S.leader(t)) n = len(gt) s = sum(S.size(t) for t in tt) ans += n*s - n print(ans)