結果

問題 No.482 あなたの名は
ユーザー titiatitia
提出日時 2023-06-05 01:28:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 360 ms / 2,000 ms
コード長 880 bytes
コンパイル時間 189 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 30,252 KB
最終ジャッジ日時 2023-08-28 08:35:19
合計ジャッジ時間 8,784 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,820 KB
testcase_01 AC 16 ms
7,756 KB
testcase_02 AC 16 ms
7,920 KB
testcase_03 AC 17 ms
7,912 KB
testcase_04 AC 16 ms
7,784 KB
testcase_05 AC 16 ms
7,768 KB
testcase_06 AC 17 ms
7,784 KB
testcase_07 AC 16 ms
8,384 KB
testcase_08 AC 17 ms
8,228 KB
testcase_09 AC 16 ms
7,920 KB
testcase_10 AC 17 ms
8,236 KB
testcase_11 AC 17 ms
8,296 KB
testcase_12 AC 17 ms
8,412 KB
testcase_13 AC 16 ms
8,212 KB
testcase_14 AC 16 ms
8,200 KB
testcase_15 AC 347 ms
30,052 KB
testcase_16 AC 352 ms
30,072 KB
testcase_17 AC 350 ms
30,224 KB
testcase_18 AC 349 ms
30,096 KB
testcase_19 AC 352 ms
30,144 KB
testcase_20 AC 354 ms
30,176 KB
testcase_21 AC 350 ms
30,048 KB
testcase_22 AC 354 ms
30,152 KB
testcase_23 AC 352 ms
30,112 KB
testcase_24 AC 360 ms
30,104 KB
testcase_25 AC 349 ms
30,252 KB
testcase_26 AC 347 ms
30,144 KB
testcase_27 AC 352 ms
30,096 KB
testcase_28 AC 344 ms
30,204 KB
testcase_29 AC 310 ms
30,204 KB
testcase_30 AC 16 ms
7,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,K=map(int,input().split())
D=list(map(int,input().split()))
for i in range(N):
    D[i]-=1

# UnionFind

Group = [i for i in range(N)] # グループ分け
Nodes = [1]*(N) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

for i in range(N):
    Union(i,D[i])

X=[]
for i in range(N):
    if find(i)==i:
        X.append(Nodes[i])

ANS=0
for x in X:
    ANS+=x-1

if ANS<=K and ANS%2==K%2:
    print("YES")
else:
    print("NO")
0