結果

問題 No.482 あなたの名は
ユーザー tookunn_1213
提出日時 2017-02-11 01:02:16
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 470 ms / 2,000 ms
コード長 693 bytes
コンパイル時間 231 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 29,620 KB
最終ジャッジ日時 2024-12-29 20:28:33
合計ジャッジ時間 8,815 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

N,K = map(int,input().split())
D = [int(i)-1 for i in input().split()]
cnt = 0
unionfind = [i for i in range(N)]
size = [1 for i in range(N)]
used = [False for i in range(N)]
def find(x):
	if unionfind[x] == x:
		return x
	unionfind[x] = find(unionfind[x])
	return unionfind[x]
def unite(x,y):
	x = find(x)
	y = find(y)
	if x == y:
		return
	if x < y:
		unionfind[y] = x
		size[x] += size[y]
	else:
		unionfind[x] = y
		size[y] += size[x]
for i in range(N):
	a = D[i]
	b = D[D[i]]
	unite(a,b)
for i in range(N):
	if not used[find(i)]:
		cnt += size[find(i)]-1
		used[find(i)] = True
def check():
	if K < cnt:
		return False
	return cnt % 2 == K % 2
if check():
	print('YES')
else:
	print('NO')
0