結果

問題 No.482 あなたの名は
ユーザー 👑 H20H20
提出日時 2020-12-03 13:27:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,569 bytes
コンパイル時間 1,213 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 112,988 KB
最終ジャッジ日時 2023-10-11 22:45:02
合計ジャッジ時間 8,304 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
71,580 KB
testcase_01 AC 96 ms
71,340 KB
testcase_02 AC 97 ms
71,624 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 97 ms
71,436 KB
testcase_06 WA -
testcase_07 AC 111 ms
76,264 KB
testcase_08 AC 102 ms
76,300 KB
testcase_09 AC 97 ms
71,176 KB
testcase_10 AC 103 ms
76,292 KB
testcase_11 AC 105 ms
76,332 KB
testcase_12 AC 105 ms
76,160 KB
testcase_13 AC 104 ms
76,528 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 245 ms
110,276 KB
testcase_18 AC 250 ms
109,444 KB
testcase_19 WA -
testcase_20 AC 243 ms
109,952 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 241 ms
109,444 KB
testcase_25 AC 247 ms
109,780 KB
testcase_26 AC 248 ms
110,168 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 179 ms
112,988 KB
testcase_30 AC 97 ms
71,096 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