結果
問題 | No.3024 全単射的 |
ユーザー |
![]() |
提出日時 | 2025-02-14 22:27:08 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 585 ms / 5,000 ms |
コード長 | 1,155 bytes |
コンパイル時間 | 302 ms |
コンパイル使用メモリ | 82,160 KB |
実行使用メモリ | 121,716 KB |
最終ジャッジ日時 | 2025-02-14 22:27:17 |
合計ジャッジ時間 | 5,376 ms |
ジャッジサーバーID (参考情報) |
judge7 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
class Union:def __init__(s, num):s.T = [-1] * numdef root(s, x):if s.T[x] < 0: return xt = s.root(s.T[x])s.T[x] = treturn tdef merge(s, x, y):an, bn = s.root(x), s.root(y)if an == bn: return Falseam, bm = -s.T[an], -s.T[bn]if am < bm: an, bn = bn, ans.T[an] += s.T[bn]s.T[bn] = anreturn Truedef size(s, x):n = s.root(x)return -s.T[n]def same(s, x, y):return s.root(x) == s.root(y)def print(s): #デバッグ用print(*s.T)ans = []for i in range(len(s.T)):ans.append(s.root(i))print(*ans)N, M = list(map(int, input().split()))XY = [list(map(int, input().split())) for _ in range(N)]cnt = 0R = {}for x, y in XY:if x not in R:R[x] = cntcnt += 1if y not in R:R[y] = cntcnt += 1D = [0] * cntuni = Union(cnt)ans = 0for x, y in XY:x = R[x]y = R[y]v = max(D[uni.root(x)], D[uni.root(y)])u = min(D[uni.root(x)], D[uni.root(y)])if uni.merge(x, y):if u != 1:ans += 1D[uni.root(x)] = velse:t = uni.root(x)if D[t] == 0:D[t] = 1ans += 1print(ans)