結果

問題 No.482 あなたの名は
ユーザー FromBooskaFromBooska
提出日時 2023-09-25 12:35:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 1,701 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 108,612 KB
最終ジャッジ日時 2023-09-25 12:35:47
合計ジャッジ時間 7,056 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,084 KB
testcase_01 AC 68 ms
71,576 KB
testcase_02 AC 68 ms
71,092 KB
testcase_03 AC 68 ms
71,448 KB
testcase_04 AC 69 ms
71,272 KB
testcase_05 AC 75 ms
71,292 KB
testcase_06 AC 70 ms
71,580 KB
testcase_07 AC 78 ms
75,500 KB
testcase_08 AC 72 ms
75,104 KB
testcase_09 AC 68 ms
71,112 KB
testcase_10 AC 72 ms
75,424 KB
testcase_11 AC 74 ms
75,480 KB
testcase_12 AC 73 ms
75,448 KB
testcase_13 AC 71 ms
75,120 KB
testcase_14 AC 72 ms
75,392 KB
testcase_15 AC 189 ms
108,612 KB
testcase_16 AC 206 ms
108,332 KB
testcase_17 AC 182 ms
108,588 KB
testcase_18 AC 181 ms
108,484 KB
testcase_19 AC 179 ms
108,608 KB
testcase_20 AC 175 ms
108,508 KB
testcase_21 AC 176 ms
108,576 KB
testcase_22 AC 178 ms
108,328 KB
testcase_23 AC 197 ms
108,476 KB
testcase_24 AC 180 ms
108,324 KB
testcase_25 AC 177 ms
108,352 KB
testcase_26 AC 183 ms
108,468 KB
testcase_27 AC 180 ms
108,324 KB
testcase_28 AC 179 ms
108,344 KB
testcase_29 AC 149 ms
108,348 KB
testcase_30 AC 69 ms
71,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UF group size-1の和が必要回数、Kが必要回数以上で(K-必要回数)%2==0ならYes

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 unite(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())
UF = UnionFind(N)
D = list(map(int, input().split()))
for i in range(N):
    d = D[i]-1
    if UF.same(i, d) == False:
        UF.unite(i, d)
        
required = 0
for root in UF.roots():
    required += UF.size(root)-1
if K >= required and (K-required)%2 == 0:
    print('YES')
else:
    print('NO')
0