結果

問題 No.482 あなたの名は
ユーザー 👑 H20H20
提出日時 2020-12-03 13:29:35
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 1,571 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 85,256 KB
実行使用メモリ 112,928 KB
最終ジャッジ日時 2023-10-11 22:46:37
合計ジャッジ時間 7,606 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
71,448 KB
testcase_01 AC 97 ms
71,560 KB
testcase_02 AC 97 ms
71,476 KB
testcase_03 AC 96 ms
71,668 KB
testcase_04 AC 96 ms
71,772 KB
testcase_05 AC 96 ms
71,684 KB
testcase_06 AC 95 ms
71,688 KB
testcase_07 AC 104 ms
76,560 KB
testcase_08 AC 104 ms
76,224 KB
testcase_09 AC 97 ms
71,704 KB
testcase_10 AC 106 ms
76,324 KB
testcase_11 AC 104 ms
76,360 KB
testcase_12 AC 105 ms
76,564 KB
testcase_13 AC 104 ms
76,268 KB
testcase_14 AC 104 ms
76,348 KB
testcase_15 AC 278 ms
109,096 KB
testcase_16 AC 244 ms
109,680 KB
testcase_17 AC 252 ms
110,340 KB
testcase_18 AC 244 ms
109,836 KB
testcase_19 AC 243 ms
109,796 KB
testcase_20 AC 244 ms
109,736 KB
testcase_21 AC 242 ms
109,812 KB
testcase_22 AC 242 ms
109,988 KB
testcase_23 AC 240 ms
109,852 KB
testcase_24 AC 240 ms
109,736 KB
testcase_25 AC 239 ms
109,960 KB
testcase_26 AC 243 ms
110,328 KB
testcase_27 AC 249 ms
110,128 KB
testcase_28 AC 240 ms
109,744 KB
testcase_29 AC 176 ms
112,928 KB
testcase_30 AC 97 ms
71,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

N,K=map(int,input().split())
D = list(map(int,input().split()))
uf = UnionFind(N)
for i in range(N):
    uf.union(i,D[i]-1)
L = uf.all_group_members()
change = 0
for l in L.values():
    change += len(l)-1
if K-change<0 or (K-change)%2==1:
    print('NO')
else:
    print('YES')
0