結果

問題 No.1290 Addition and Subtraction Operation
ユーザー vwxyzvwxyz
提出日時 2023-03-21 21:00:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 474 ms / 2,000 ms
コード長 3,006 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 12,184 KB
実行使用メモリ 45,080 KB
最終ジャッジ日時 2023-10-18 18:35:02
合計ジャッジ時間 21,551 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,356 KB
testcase_01 AC 32 ms
10,356 KB
testcase_02 AC 32 ms
10,356 KB
testcase_03 AC 31 ms
10,356 KB
testcase_04 AC 33 ms
10,356 KB
testcase_05 AC 32 ms
10,356 KB
testcase_06 AC 31 ms
10,356 KB
testcase_07 AC 31 ms
10,356 KB
testcase_08 AC 31 ms
10,356 KB
testcase_09 AC 31 ms
10,356 KB
testcase_10 AC 31 ms
10,356 KB
testcase_11 AC 32 ms
10,356 KB
testcase_12 AC 31 ms
10,356 KB
testcase_13 AC 31 ms
10,356 KB
testcase_14 AC 31 ms
10,356 KB
testcase_15 AC 32 ms
10,356 KB
testcase_16 AC 30 ms
10,356 KB
testcase_17 AC 31 ms
10,356 KB
testcase_18 AC 31 ms
10,356 KB
testcase_19 AC 30 ms
10,356 KB
testcase_20 AC 31 ms
10,356 KB
testcase_21 AC 31 ms
10,356 KB
testcase_22 AC 31 ms
10,356 KB
testcase_23 AC 31 ms
10,356 KB
testcase_24 AC 32 ms
10,356 KB
testcase_25 AC 32 ms
10,356 KB
testcase_26 AC 32 ms
10,356 KB
testcase_27 AC 31 ms
10,356 KB
testcase_28 AC 32 ms
10,356 KB
testcase_29 AC 32 ms
10,356 KB
testcase_30 AC 32 ms
10,356 KB
testcase_31 AC 31 ms
10,356 KB
testcase_32 AC 31 ms
10,356 KB
testcase_33 AC 32 ms
10,356 KB
testcase_34 AC 31 ms
10,356 KB
testcase_35 AC 31 ms
10,356 KB
testcase_36 AC 31 ms
10,356 KB
testcase_37 AC 31 ms
10,356 KB
testcase_38 AC 32 ms
10,356 KB
testcase_39 AC 31 ms
10,356 KB
testcase_40 AC 31 ms
10,356 KB
testcase_41 AC 31 ms
10,356 KB
testcase_42 AC 234 ms
10,428 KB
testcase_43 AC 235 ms
10,428 KB
testcase_44 AC 232 ms
10,428 KB
testcase_45 AC 233 ms
10,428 KB
testcase_46 AC 233 ms
10,428 KB
testcase_47 AC 233 ms
10,444 KB
testcase_48 AC 232 ms
10,444 KB
testcase_49 AC 236 ms
10,444 KB
testcase_50 AC 235 ms
10,444 KB
testcase_51 AC 233 ms
10,444 KB
testcase_52 AC 236 ms
10,444 KB
testcase_53 AC 241 ms
10,444 KB
testcase_54 AC 233 ms
10,444 KB
testcase_55 AC 235 ms
10,444 KB
testcase_56 AC 235 ms
10,444 KB
testcase_57 AC 299 ms
35,816 KB
testcase_58 AC 349 ms
33,960 KB
testcase_59 AC 297 ms
36,068 KB
testcase_60 AC 444 ms
24,736 KB
testcase_61 AC 400 ms
27,688 KB
testcase_62 AC 415 ms
26,908 KB
testcase_63 AC 347 ms
33,960 KB
testcase_64 AC 248 ms
41,752 KB
testcase_65 AC 287 ms
36,872 KB
testcase_66 AC 388 ms
32,108 KB
testcase_67 AC 474 ms
24,988 KB
testcase_68 AC 406 ms
27,436 KB
testcase_69 AC 354 ms
33,956 KB
testcase_70 AC 338 ms
34,492 KB
testcase_71 AC 377 ms
32,904 KB
testcase_72 AC 286 ms
36,872 KB
testcase_73 AC 389 ms
28,744 KB
testcase_74 AC 237 ms
41,740 KB
testcase_75 AC 272 ms
42,084 KB
testcase_76 AC 343 ms
34,488 KB
testcase_77 AC 432 ms
31,896 KB
testcase_78 AC 418 ms
33,016 KB
testcase_79 AC 345 ms
38,948 KB
testcase_80 AC 459 ms
29,948 KB
testcase_81 AC 462 ms
30,164 KB
testcase_82 AC 390 ms
37,032 KB
testcase_83 AC 455 ms
30,188 KB
testcase_84 AC 437 ms
31,428 KB
testcase_85 AC 299 ms
39,928 KB
testcase_86 AC 259 ms
45,080 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