結果

問題 No.482 あなたの名は
ユーザー lloyzlloyz
提出日時 2022-12-06 00:27:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 2,180 bytes
コンパイル時間 1,916 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 107,008 KB
最終ジャッジ日時 2024-04-20 22:06:37
合計ジャッジ時間 5,188 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
53,760 KB
testcase_01 AC 49 ms
53,888 KB
testcase_02 AC 48 ms
53,632 KB
testcase_03 AC 48 ms
54,016 KB
testcase_04 AC 48 ms
53,760 KB
testcase_05 AC 48 ms
54,016 KB
testcase_06 AC 48 ms
53,632 KB
testcase_07 AC 56 ms
61,312 KB
testcase_08 AC 53 ms
59,264 KB
testcase_09 AC 49 ms
54,272 KB
testcase_10 AC 53 ms
59,648 KB
testcase_11 AC 54 ms
59,648 KB
testcase_12 AC 56 ms
60,800 KB
testcase_13 AC 54 ms
59,520 KB
testcase_14 AC 53 ms
54,784 KB
testcase_15 AC 174 ms
106,368 KB
testcase_16 AC 163 ms
106,652 KB
testcase_17 AC 170 ms
106,996 KB
testcase_18 AC 166 ms
106,240 KB
testcase_19 AC 166 ms
106,240 KB
testcase_20 AC 171 ms
106,496 KB
testcase_21 AC 175 ms
106,752 KB
testcase_22 AC 184 ms
106,240 KB
testcase_23 AC 180 ms
106,624 KB
testcase_24 AC 171 ms
106,936 KB
testcase_25 AC 172 ms
106,880 KB
testcase_26 AC 165 ms
106,468 KB
testcase_27 AC 166 ms
106,752 KB
testcase_28 AC 168 ms
106,240 KB
testcase_29 AC 134 ms
107,008 KB
testcase_30 AC 50 ms
54,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素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を含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素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):
        '''
        要素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())
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
A = list(map(int, input().split()))
UF = UnionFind(n + 1)
for i in range(n):
    UF.union(A[i], i + 1)
seen = set()
G = []
roots = UF.roots()
for root in roots:
    G.append(UF.size(root))

cnt = sum(G) - len(G)
k -= cnt
if k < 0:
    print("NO")
    exit()
if k % 2 == 0:
    print("YES")
else:
    print("NO")
0