結果
| 問題 |
No.3024 全単射的
|
| コンテスト | |
| ユーザー |
nasutarou1341
|
| 提出日時 | 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] * num
def root(s, x):
if s.T[x] < 0: return x
t = s.root(s.T[x])
s.T[x] = t
return t
def merge(s, x, y):
an, bn = s.root(x), s.root(y)
if an == bn: return False
am, bm = -s.T[an], -s.T[bn]
if am < bm: an, bn = bn, an
s.T[an] += s.T[bn]
s.T[bn] = an
return True
def 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 = 0
R = {}
for x, y in XY:
if x not in R:
R[x] = cnt
cnt += 1
if y not in R:
R[y] = cnt
cnt += 1
D = [0] * cnt
uni = Union(cnt)
ans = 0
for 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 += 1
D[uni.root(x)] = v
else:
t = uni.root(x)
if D[t] == 0:
D[t] = 1
ans += 1
print(ans)
nasutarou1341