結果

問題 No.482 あなたの名は
ユーザー 👑 rin204rin204
提出日時 2022-01-20 01:30:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 1,534 bytes
コンパイル時間 1,946 ms
コンパイル使用メモリ 86,516 KB
実行使用メモリ 108,352 KB
最終ジャッジ日時 2023-08-15 08:18:26
合計ジャッジ時間 5,192 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,032 KB
testcase_01 AC 61 ms
70,916 KB
testcase_02 AC 61 ms
71,192 KB
testcase_03 AC 66 ms
71,192 KB
testcase_04 AC 61 ms
71,168 KB
testcase_05 AC 60 ms
71,000 KB
testcase_06 AC 60 ms
71,136 KB
testcase_07 AC 66 ms
75,100 KB
testcase_08 AC 68 ms
75,036 KB
testcase_09 AC 62 ms
70,932 KB
testcase_10 AC 66 ms
74,688 KB
testcase_11 AC 69 ms
74,916 KB
testcase_12 AC 69 ms
75,064 KB
testcase_13 AC 67 ms
75,192 KB
testcase_14 AC 63 ms
71,216 KB
testcase_15 AC 158 ms
107,996 KB
testcase_16 AC 150 ms
108,176 KB
testcase_17 AC 158 ms
108,016 KB
testcase_18 AC 161 ms
108,056 KB
testcase_19 AC 161 ms
108,000 KB
testcase_20 AC 155 ms
108,232 KB
testcase_21 AC 158 ms
108,168 KB
testcase_22 AC 154 ms
108,016 KB
testcase_23 AC 152 ms
107,936 KB
testcase_24 AC 156 ms
108,016 KB
testcase_25 AC 152 ms
107,972 KB
testcase_26 AC 154 ms
108,064 KB
testcase_27 AC 152 ms
108,060 KB
testcase_28 AC 156 ms
108,184 KB
testcase_29 AC 116 ms
108,352 KB
testcase_30 AC 62 ms
71,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = 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
        self.group -= 1
        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 self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n, k = map(int, input().split())
D = list(map(int, input().split()))
UF = UnionFind(n)
for i, d in enumerate(D):
    UF.union(i, d - 1)

ans = 0
for r in UF.roots():
    ans += UF.size(r) - 1

if ans <= k and ans % 2 == k % 2:
    print("YES")
else:
    print("NO")
    

0