結果

問題 No.482 あなたの名は
コンテスト
ユーザー yomo3
提出日時 2020-04-06 16:24:20
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 655 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 738 ms
コンパイル使用メモリ 20,700 KB
実行使用メモリ 37,068 KB
最終ジャッジ日時 2026-03-28 01:43:05
合計ジャッジ時間 6,971 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

class UnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n)]
    def find(self, x):
        if self.parent[x] == x:
            return x
        self.parent[x] = self.find(self.parent[x])
        return self.parent[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        self.parent[x] = y

N, K = map(int, input().split())
D = list(map(int, input().split()))

uf = UnionFind(N + 1)

for i in range(N):
    uf.union(i + 1, D[i])

s = set(uf.find(i) for i in range(1, N + 1))
cnt = N - len(s)

yes = cnt <= K and cnt % 2 == K % 2
print('YES' if yes else 'NO')
0