結果

問題 No.3113 How Many Liars Are There?
ユーザー 👑 獅子座じゃない人獅子座じゃない人
提出日時 2024-03-29 16:23:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,263 ms / 2,000 ms
コード長 3,956 bytes
コンパイル時間 139 ms
コンパイル使用メモリ 82,072 KB
実行使用メモリ 96,400 KB
最終ジャッジ日時 2024-10-03 05:18:01
合計ジャッジ時間 23,821 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
69,596 KB
testcase_01 AC 54 ms
68,792 KB
testcase_02 AC 56 ms
71,808 KB
testcase_03 AC 57 ms
71,704 KB
testcase_04 AC 936 ms
95,748 KB
testcase_05 AC 770 ms
95,232 KB
testcase_06 AC 646 ms
95,488 KB
testcase_07 AC 821 ms
95,616 KB
testcase_08 AC 975 ms
95,752 KB
testcase_09 AC 924 ms
95,708 KB
testcase_10 AC 805 ms
95,236 KB
testcase_11 AC 492 ms
95,112 KB
testcase_12 AC 1,033 ms
96,212 KB
testcase_13 AC 833 ms
95,476 KB
testcase_14 AC 946 ms
95,936 KB
testcase_15 AC 1,263 ms
96,400 KB
testcase_16 AC 686 ms
94,584 KB
testcase_17 AC 683 ms
94,716 KB
testcase_18 AC 1,035 ms
95,948 KB
testcase_19 AC 1,029 ms
95,536 KB
testcase_20 AC 145 ms
81,248 KB
testcase_21 AC 145 ms
81,036 KB
testcase_22 AC 147 ms
81,044 KB
testcase_23 AC 148 ms
81,488 KB
testcase_24 AC 124 ms
79,768 KB
testcase_25 AC 80 ms
78,620 KB
testcase_26 AC 86 ms
78,292 KB
testcase_27 AC 140 ms
79,900 KB
testcase_28 AC 223 ms
82,388 KB
testcase_29 AC 143 ms
80,060 KB
testcase_30 AC 791 ms
91,780 KB
testcase_31 AC 53 ms
69,012 KB
testcase_32 AC 367 ms
87,444 KB
testcase_33 AC 449 ms
86,352 KB
testcase_34 AC 144 ms
80,888 KB
testcase_35 AC 244 ms
83,396 KB
testcase_36 AC 696 ms
94,824 KB
testcase_37 AC 165 ms
80,440 KB
testcase_38 AC 296 ms
88,720 KB
testcase_39 AC 661 ms
88,892 KB
testcase_40 AC 100 ms
79,280 KB
testcase_41 AC 273 ms
86,092 KB
testcase_42 AC 244 ms
84,340 KB
testcase_43 AC 588 ms
87,212 KB
testcase_44 AC 57 ms
68,036 KB
testcase_45 AC 54 ms
70,008 KB
testcase_46 AC 55 ms
69,500 KB
testcase_47 AC 54 ms
68,480 KB
testcase_48 AC 55 ms
68,884 KB
testcase_49 AC 59 ms
69,576 KB
testcase_50 AC 58 ms
69,084 KB
testcase_51 AC 59 ms
69,448 KB
testcase_52 AC 64 ms
69,336 KB
testcase_53 AC 64 ms
69,380 KB
testcase_54 AC 66 ms
69,592 KB
testcase_55 AC 65 ms
68,772 KB
testcase_56 AC 64 ms
69,372 KB
testcase_57 AC 57 ms
69,060 KB
testcase_58 AC 58 ms
69,316 KB
testcase_59 AC 58 ms
69,984 KB
testcase_60 AC 55 ms
68,684 KB
testcase_61 AC 58 ms
69,856 KB
testcase_62 AC 57 ms
69,524 KB
testcase_63 AC 56 ms
68,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing

class DSU:
    '''
    Implement (union by size) + (path halving)

    Reference:
    Zvi Galil and Giuseppe F. Italiano,
    Data structures and algorithms for disjoint set union problems
    '''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a: int, b: int) -> int:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        x = self.leader(a)
        y = self.leader(b)

        if x == y:
            return x

        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x

        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x

        return x

    def same(self, a: int, b: int) -> bool:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        return self.leader(a) == self.leader(b)

    def leader(self, a: int) -> int:
        assert 0 <= a < self._n

        parent = self.parent_or_size[a]
        while parent >= 0:
            if self.parent_or_size[parent] < 0:
                return parent
            self.parent_or_size[a], a, parent = (
                self.parent_or_size[parent],
                self.parent_or_size[parent],
                self.parent_or_size[self.parent_or_size[parent]]
            )

        return a

    def size(self, a: int) -> int:
        assert 0 <= a < self._n

        return -self.parent_or_size[self.leader(a)]

    def groups(self) -> typing.List[typing.List[int]]:
        leader_buf = [self.leader(i) for i in range(self._n)]

        result: typing.List[typing.List[int]] = [[] for _ in range(self._n)]
        for i in range(self._n):
            result[leader_buf[i]].append(i)

        return list(filter(lambda r: r, result))

from collections import deque

n,k=map(int, input().split())
p=list(map(lambda x: int(x)-1, input().split()))
a=list(map(int, input().split()))

indeg=[0]*n
uf=DSU(n)
is_root=[False]*n
is_root_next=[False]*n

for v in range(n):
    if uf.same(v, p[v]):
        is_root[v]=True
        is_root_next[p[v]]=True
    else:
        indeg[p[v]]+=1
    uf.merge(v, p[v])

ans=[False]*(2*n+1)
dp=[[0 for _ in range(2*n+1)] for _ in range(n)]
size=[1]*n
ans_size=0
groups=uf.groups()
ans[0]=True

for group in groups:
    queue=deque()
    for v in group:
        if indeg[v]==0:
            queue.appendleft(v)
        for c_v in [0,1]:
            for c_p in [0,1]:
                for c_s in [0,1]:
                    dp[v][c_v+(a[v]+c_v+c_p)%2]|=1<<(4*c_v+2*c_p+c_s)

    while len(queue)>0:
        v=queue.pop()

        if is_root[v]:
            dp_p=[False]*(2*(size[v]+ans_size)+1)
            for i in range(2*size[v]+1):
                for c_v in [0,1]:
                    for c_p in [0,1]:
                        for j in range(2*ans_size+1):
                            dp_p[i+j]|=(dp[v][i]>>(4*c_v+3*c_p))&ans[j]&1

            ans_size+=size[v]
            for i in range(2*ans_size+1):
                ans[i]=dp_p[i]

        else:
            dp_p=[0 for _ in range(2*(size[v]+size[p[v]])+1)]
            for i in range(2*size[v]+1):
                for c_v in [0,1]:
                    for c_p in [0,1]:
                        for c_pp in [0,1]:
                            for j in range(2*size[p[v]]+1):
                                if is_root_next[v]:
                                    dp_p[i+j]|=((dp[v][i]>>(5*c_v+2*c_p))&(dp[p[v]][j]>>(4*c_p+2*c_pp+c_v))&1)<<(4*c_p+2*c_pp+c_v)
                                else:
                                    for c_s in [0,1]:
                                        dp_p[i+j]|=((dp[v][i]>>(4*c_v+2*c_p+c_s))&(dp[p[v]][j]>>(4*c_p+2*c_pp+c_s))&1)<<(4*c_p+2*c_pp+c_s)

            size[p[v]]+=size[v]
            for i in range(2*size[p[v]]+1):
                dp[p[v]][i]=dp_p[i]

            indeg[p[v]]-=1
            if indeg[p[v]]==0:
                queue.appendleft(p[v])

print("Yes" if ans[k] else "No")
0