結果

問題 No.1508 Avoid being hit
ユーザー 👑 KazunKazun
提出日時 2021-05-14 22:56:22
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,852 bytes
コンパイル時間 242 ms
コンパイル使用メモリ 82,460 KB
実行使用メモリ 105,844 KB
最終ジャッジ日時 2024-04-10 02:11:11
合計ジャッジ時間 11,280 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 37 ms
52,684 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 36 ms
54,172 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 36 ms
53,076 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 35 ms
53,388 KB
testcase_16 AC 149 ms
101,308 KB
testcase_17 AC 121 ms
88,252 KB
testcase_18 AC 101 ms
83,688 KB
testcase_19 AC 146 ms
97,656 KB
testcase_20 AC 146 ms
105,844 KB
testcase_21 AC 124 ms
94,980 KB
testcase_22 AC 139 ms
101,572 KB
testcase_23 AC 138 ms
101,972 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#AC or RE ならば方針OK

class Union_Find():
    def __init__(self,N):
        """0,1,...,N-1を要素として初期化する.

        N:要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.rank=[0]*N

    def find(self, x):
        """要素xの属している族を調べる.

        x:要素
        """
        V=[]
        while self.parents[x]>=0:
            V.append(x)
            x=self.parents[x]

        for v in V:
            self.parents[v]=x
        return x

    def union(self, x, y):
        """要素x,yを同一視する.

        x,y:要素
        """
        x=self.find(x)
        y=self.find(y)

        if x==y:
            return

        if self.rank[x]<self.rank[y]:
            x,y=y,x

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

        if self.rank[x]==self.rank[y]:
            self.rank[x]+=1

    def size(self, x):
        """要素xの属している要素の数.

        x:要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """要素x,yは同一視されているか?

        x,y:要素
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """要素xが属している族の要素.
        ※族の要素の個数が欲しいときはsizeを使うこと!!

        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):
        """全ての族の出力
        """
        X={r:[] for r in self.roots()}
        for k in range(self.n):
            X[self.find(k)].append(k)
        return X

    def refresh(self):
        for i in range(self.n):
            _=self.find(i)

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

    def __repr__(self):
        return self.__str__()
#==================================================
N,Q=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

for i in range(Q):
    if A[i]>B[i]:
        A[i],B[i]=B[i],A[i]

U=Union_Find(2*Q+2)
s,t=0,2*Q+1
for i in range(Q):
    if A[i]==1: U.union(s,2*i+1)
    if B[i]==1: U.union(s,2*i+2)

    if A[i]==N: U.union(2*i+2,t)
    if B[i]==N: U.union(2*i+2,t)

    if B[i]-A[i]==1: U.union(2*i+1,2*i+2)

    if i>=1:
        j=i-1
        if A[i]==A[j]: U.union(2*i+1,2*j+1)
        if A[i]==B[j]: U.union(2*i+1,2*j+2)
        if B[i]==A[j]: U.union(2*i+2,2*j+1)
        if B[i]==B[j]: U.union(2*i+2,2*j+2)

if U.same(s,t):
    exit(print("NO"))
print("YES")

x=1/0
0