結果
問題 | No.1390 Get together |
ユーザー |
|
提出日時 | 2021-02-12 22:04:57 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,146 bytes |
コンパイル時間 | 79 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 83,840 KB |
最終ジャッジ日時 | 2024-07-19 21:44:10 |
合計ジャッジ時間 | 25,774 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 18 RE * 11 |
ソースコード
class UnionFind():def __init__(self, n):self.n = nself.par = list(range(self.n))self.rank = [1] * nself.count = ndef find(self, x):if self.par[x] == x:return xelse:self.par[x] = self.find(self.par[x])return self.par[x]def unite(self, x, y):p = self.find(x)q = self.find(y)if p == q:return Noneif p > q:p, q = q, pself.rank[p] += self.rank[q]self.par[q] = pself.count -= 1def same(self, x, y):return self.find(x) == self.find(y)def size(self, x):return self.rank[x]def count(self):return self.countn, m = map(int, input().split())UF = UnionFind(n)bc = [list(map(int, input().split())) for i in range(n)]d = [[] for i in range(n)]for b, c in bc:d[c - 1].append(b - 1)for i in range(n):d[i].sort()ans = 0for i in range(n):for j in range(len(d[i]) - 1):if UF.same(d[i][j], d[i][j + 1]):continueUF.unite(d[i][j], d[i][j + 1])ans += 1print(ans)