結果
問題 | No.2740 Old Maid |
ユーザー |
👑 |
提出日時 | 2024-04-20 13:15:23 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 280 ms / 2,000 ms |
コード長 | 1,430 bytes |
コンパイル時間 | 211 ms |
コンパイル使用メモリ | 82,180 KB |
実行使用メモリ | 113,148 KB |
最終ジャッジ日時 | 2024-10-12 08:41:16 |
合計ジャッジ時間 | 12,469 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 62 |
ソースコード
class UnionFind:def __init__(self, n):self.n = nself.par = [-1] * nself.group_ = nself.R = [i for i in range(n)]def find(self, x):if self.par[x] < 0:return xlst = []while self.par[x] >= 0:lst.append(x)x = self.par[x]for y in lst:self.par[y] = xreturn xdef unite(self, x, y):x = self.find(x)y = self.find(y)if x == y:return Falseif self.par[x] > self.par[y]:x, y = y, xself.par[x] += self.par[y]self.par[y] = xself.R[x] = max(self.R[x], self.R[y])self.group_ -= 1return Truedef size(self, x):return -self.par[self.find(x)]def same(self, x, y):return self.find(x) == self.find(y)@propertydef group(self):return self.group_n = int(input())P = list(map(int, input().split()))for i in range(n):P[i] -= 1invP = [0] * nfor i in range(n):invP[P[i]] = iQ = []p = 0UF = UnionFind(n)used = [False] * nwhile p < n:if used[p]:p += 1continuex = invP[p]if UF.R[UF.find(x)] == n - 1:p += 1continuey = UF.R[UF.find(x)] + 1q = P[y]Q += [p + 1, q + 1]UF.unite(x, x - 1)UF.unite(y, y - 1)used[p] = Trueused[q] = Trueprint(*Q)