結果

問題 No.482 あなたの名は
ユーザー yomo3
提出日時 2020-04-06 16:24:20
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 655 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 33,424 KB
最終ジャッジ日時 2024-07-06 12:28:41
合計ジャッジ時間 6,108 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

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