結果

問題 No.1290 Addition and Subtraction Operation
ユーザー vwxyzvwxyz
提出日時 2023-03-21 21:00:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 287 ms / 2,000 ms
コード長 3,006 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 109,824 KB
最終ジャッジ日時 2024-09-18 14:30:36
合計ジャッジ時間 13,823 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
54,400 KB
testcase_01 AC 46 ms
54,016 KB
testcase_02 AC 44 ms
54,400 KB
testcase_03 AC 43 ms
54,720 KB
testcase_04 AC 43 ms
54,356 KB
testcase_05 AC 43 ms
54,400 KB
testcase_06 AC 44 ms
53,888 KB
testcase_07 AC 43 ms
54,784 KB
testcase_08 AC 44 ms
53,888 KB
testcase_09 AC 45 ms
54,016 KB
testcase_10 AC 44 ms
53,888 KB
testcase_11 AC 45 ms
53,888 KB
testcase_12 AC 46 ms
54,400 KB
testcase_13 AC 43 ms
54,528 KB
testcase_14 AC 45 ms
54,400 KB
testcase_15 AC 45 ms
54,400 KB
testcase_16 AC 45 ms
54,400 KB
testcase_17 AC 46 ms
54,144 KB
testcase_18 AC 43 ms
54,400 KB
testcase_19 AC 44 ms
54,528 KB
testcase_20 AC 44 ms
54,528 KB
testcase_21 AC 44 ms
54,400 KB
testcase_22 AC 44 ms
54,400 KB
testcase_23 AC 44 ms
54,272 KB
testcase_24 AC 43 ms
54,528 KB
testcase_25 AC 44 ms
54,400 KB
testcase_26 AC 42 ms
54,528 KB
testcase_27 AC 44 ms
54,528 KB
testcase_28 AC 45 ms
54,528 KB
testcase_29 AC 43 ms
54,784 KB
testcase_30 AC 44 ms
53,888 KB
testcase_31 AC 43 ms
54,528 KB
testcase_32 AC 42 ms
54,400 KB
testcase_33 AC 45 ms
54,016 KB
testcase_34 AC 44 ms
54,528 KB
testcase_35 AC 44 ms
54,784 KB
testcase_36 AC 45 ms
54,400 KB
testcase_37 AC 45 ms
54,144 KB
testcase_38 AC 43 ms
54,144 KB
testcase_39 AC 43 ms
54,272 KB
testcase_40 AC 42 ms
54,528 KB
testcase_41 AC 42 ms
54,400 KB
testcase_42 AC 116 ms
76,800 KB
testcase_43 AC 118 ms
76,640 KB
testcase_44 AC 118 ms
76,672 KB
testcase_45 AC 120 ms
76,544 KB
testcase_46 AC 124 ms
76,800 KB
testcase_47 AC 119 ms
76,160 KB
testcase_48 AC 120 ms
76,032 KB
testcase_49 AC 121 ms
76,160 KB
testcase_50 AC 120 ms
76,160 KB
testcase_51 AC 119 ms
76,800 KB
testcase_52 AC 119 ms
76,032 KB
testcase_53 AC 120 ms
76,800 KB
testcase_54 AC 120 ms
76,032 KB
testcase_55 AC 118 ms
76,356 KB
testcase_56 AC 118 ms
76,420 KB
testcase_57 AC 225 ms
108,288 KB
testcase_58 AC 256 ms
107,648 KB
testcase_59 AC 216 ms
108,288 KB
testcase_60 AC 283 ms
108,160 KB
testcase_61 AC 278 ms
107,904 KB
testcase_62 AC 282 ms
108,032 KB
testcase_63 AC 244 ms
108,288 KB
testcase_64 AC 173 ms
108,032 KB
testcase_65 AC 208 ms
108,288 KB
testcase_66 AC 267 ms
108,416 KB
testcase_67 AC 282 ms
108,544 KB
testcase_68 AC 271 ms
107,776 KB
testcase_69 AC 246 ms
108,416 KB
testcase_70 AC 237 ms
108,032 KB
testcase_71 AC 259 ms
107,776 KB
testcase_72 AC 203 ms
108,288 KB
testcase_73 AC 272 ms
108,416 KB
testcase_74 AC 162 ms
109,824 KB
testcase_75 AC 188 ms
108,416 KB
testcase_76 AC 246 ms
107,904 KB
testcase_77 AC 276 ms
109,056 KB
testcase_78 AC 273 ms
108,800 KB
testcase_79 AC 236 ms
108,800 KB
testcase_80 AC 287 ms
108,544 KB
testcase_81 AC 285 ms
109,056 KB
testcase_82 AC 269 ms
108,416 KB
testcase_83 AC 278 ms
108,928 KB
testcase_84 AC 279 ms
108,800 KB
testcase_85 AC 222 ms
109,056 KB
testcase_86 AC 179 ms
108,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
from collections import defaultdict

class UnionFind:
    def __init__(self,N,label=None,f=None,weighted=False):
        self.N=N
        self.parents=[None]*self.N
        self.size=[1]*self.N
        self.roots={i for i in range(self.N)}
        self.label=label
        if self.label!=None:
            self.label=[x for x in label]
        self.f=f
        self.weighted=weighted
        if self.weighted:
            self.weight=[0]*self.N

    def Find(self,x):
        stack=[]
        while self.parents[x]!=None:
            stack.append(x)
            x=self.parents[x]
        if self.weighted:
            w=0
            for y in stack[::-1]:
                self.parents[y]=x
                w+=self.weight[y]
                self.weight[y]=w
        else:
            for y in stack[::-1]:
                self.parents[y]=x
        return x

    def Union(self,x,y,w=None):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x==root_y:
            if self.weighted:
                if self.weight[y]-self.weight[x]==w:
                    return True
                else:
                    return False
        else:
            if self.size[root_x]<self.size[root_y]:
                x,y=y,x
                root_x,root_y=root_y,root_x
                if self.weighted:
                    w=-w
            self.parents[root_y]=root_x
            self.size[root_x]+=self.size[root_y]
            self.roots.remove(root_y)
            if self.label!=None:
                self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
            if self.weighted:
                self.weight[root_y]=w+self.weight[x]-self.weight[y]

    def Size(self,x):
        return self.size[self.Find(x)]

    def Same(self,x,y):
        return self.Find(x)==self.Find(y)

    def Label(self,x):
        return self.label[self.Find(x)]

    def Weight(self,x,y):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x!=root_y:
            return None
        return self.weight[y]-self.weight[x]

    def Roots(self):
        return list(self.roots)

    def Linked_Components_Count(self):
        return len(self.roots)

    def Linked_Components(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return linked_components

    def __str__(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return "\n".join(f"{r}: {m}" for r,m in linked_components.items())

N,M=map(int,readline().split())
B=[0]+list(map(int,readline().split()))+[0]
for i in range(1,N+1,2):
    B[i]*=-1
diff=[B[i+1]-B[i] for i in range(N+1)]
UF=UnionFind(N+1)
for m in range(M):
    l,r=map(int,readline().split())
    l-=1
    UF.Union(l,r)
ans="YES"
for r,lst in UF.Linked_Components().items():
    if sum(diff[x] for x in lst):
        ans="NO"
print(ans)
0