結果

問題 No.482 あなたの名は
ユーザー rlangevinrlangevin
提出日時 2023-01-13 19:52:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,112 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 87,164 KB
実行使用メモリ 111,588 KB
最終ジャッジ日時 2023-08-26 01:48:07
合計ジャッジ時間 6,385 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,292 KB
testcase_01 AC 72 ms
71,268 KB
testcase_02 AC 71 ms
71,336 KB
testcase_03 AC 70 ms
71,288 KB
testcase_04 AC 71 ms
70,996 KB
testcase_05 AC 71 ms
71,312 KB
testcase_06 AC 72 ms
71,288 KB
testcase_07 AC 78 ms
75,472 KB
testcase_08 AC 77 ms
75,292 KB
testcase_09 AC 73 ms
71,404 KB
testcase_10 AC 75 ms
74,876 KB
testcase_11 AC 80 ms
75,312 KB
testcase_12 AC 77 ms
75,288 KB
testcase_13 AC 76 ms
75,232 KB
testcase_14 AC 75 ms
74,920 KB
testcase_15 AC 216 ms
111,440 KB
testcase_16 AC 211 ms
111,304 KB
testcase_17 AC 212 ms
111,172 KB
testcase_18 AC 210 ms
111,428 KB
testcase_19 AC 217 ms
111,112 KB
testcase_20 AC 213 ms
111,160 KB
testcase_21 AC 215 ms
111,428 KB
testcase_22 AC 212 ms
111,228 KB
testcase_23 AC 205 ms
111,312 KB
testcase_24 AC 208 ms
111,500 KB
testcase_25 AC 209 ms
111,484 KB
testcase_26 AC 208 ms
111,164 KB
testcase_27 AC 207 ms
111,588 KB
testcase_28 AC 211 ms
111,112 KB
testcase_29 AC 150 ms
111,212 KB
testcase_30 AC 72 ms
71,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

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

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]

N, K = map(int, input().split())
D = list(map(int, input().split()))
U = UnionFind(N)
for i, d in enumerate(D):
    U.union(i, d - 1)
    
S = set()
for i in range(N):
    S.add(U.find(i))
    
for s in S:
    K -= U.get_size(s) - 1
    
print("YES") if K >= 0 and K % 2 == 0 else print("NO")
0