結果
| 問題 |
No.3024 全単射的
|
| コンテスト | |
| ユーザー |
nasutarou1341
|
| 提出日時 | 2025-02-14 22:24:28 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,025 bytes |
| コンパイル時間 | 305 ms |
| コンパイル使用メモリ | 82,288 KB |
| 実行使用メモリ | 92,768 KB |
| 最終ジャッジ日時 | 2025-02-14 22:24:35 |
| 合計ジャッジ時間 | 4,479 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 RE * 4 |
ソースコード
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)]
D = [0] * M
uni = Union(M)
ans = 0
for x, y in XY:
x -= 1
y -= 1
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