結果

問題 No.482 あなたの名は
ユーザー fgshun
提出日時 2021-01-04 11:13:54
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 664 ms / 2,000 ms
コード長 948 bytes
コンパイル時間 267 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 93,988 KB
最終ジャッジ日時 2024-10-14 13:36:19
合計ジャッジ時間 11,841 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys


def _solve(N, K, D):
    G = {}
    for i, v in enumerate(D, 1):
        G.setdefault(i, set()).add(v)
        G.setdefault(v, set()).add(i)
    # print(G)

    c = 0
    used = set()
    for i in range(1, N + 1):
        if i in used:
            continue
        if i not in G:
            continue
        e = 0
        s = [i]
        while s:
            cur = s.pop()
            for j in G[cur]:
                if j in used:
                    continue
                used.add(j)
                e += 1
                if j not in G:
                    continue
                s.append(j)
        c += e - 1
    # print(K, c)
    return K >= c and (K - c) % 2 == 0


def solve(in_):
    N, K = map(int, next(in_).split())
    D = list(map(int, next(in_).split()))
    return _solve(N, K, D)
    

def main():
    answer = solve(sys.stdin.buffer)
    print('YES' if answer else 'NO')


if __name__ == '__main__':
    main()
0