結果
問題 | No.1390 Get together |
ユーザー |
![]() |
提出日時 | 2021-02-12 21:32:35 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 324 ms / 2,000 ms |
コード長 | 1,278 bytes |
コンパイル時間 | 196 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 102,292 KB |
最終ジャッジ日時 | 2024-07-19 20:27:16 |
合計ジャッジ時間 | 6,382 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
from collections import defaultdictimport sysinput = sys.stdin.buffer.readlinesys.setrecursionlimit(10 ** 7)class UF_tree:def __init__(self, n):self.root = [-1] * (n + 1)self.rank = [0] * (n + 1)def find(self, x):if self.root[x] < 0:return xself.root[x] = self.find(self.root[x])return self.root[x]def isSame(self, x, y):return self.find(x) == self.find(y)def unite(self, x, y):x = self.find(x)y = self.find(y)if x == y:return Falseif self.rank[x] < self.rank[y]:self.root[y] += self.root[x]self.root[x] = yelse:self.root[x] += self.root[y]self.root[y] = xif self.rank[x] == self.rank[y]:self.rank[x] += 1return Truedef size(self, x):return -self.root[self.find(x)]N, M = map(int, input().split())d = defaultdict(list)for _ in range(N):b, c = map(int, input().split())b -= 1c -= 1d[c].append(b)ans = 0uf = UF_tree(M)for k, val in d.items():if len(val) == 1:continuea = val.pop()while val:b = val.pop()ans += uf.unite(a, b)a = bprint(ans)