結果

問題 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  
実行時間 519 ms / 2,000 ms
コード長 3,006 bytes
コンパイル時間 224 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 45,752 KB
最終ジャッジ日時 2024-09-18 14:30:21
合計ジャッジ時間 21,926 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
11,008 KB
testcase_01 AC 34 ms
11,008 KB
testcase_02 AC 32 ms
11,008 KB
testcase_03 AC 32 ms
11,008 KB
testcase_04 AC 31 ms
11,008 KB
testcase_05 AC 31 ms
10,880 KB
testcase_06 AC 31 ms
11,008 KB
testcase_07 AC 32 ms
11,008 KB
testcase_08 AC 32 ms
11,008 KB
testcase_09 AC 33 ms
11,008 KB
testcase_10 AC 33 ms
11,008 KB
testcase_11 AC 32 ms
11,008 KB
testcase_12 AC 32 ms
11,008 KB
testcase_13 AC 31 ms
11,008 KB
testcase_14 AC 32 ms
11,008 KB
testcase_15 AC 32 ms
11,008 KB
testcase_16 AC 31 ms
10,880 KB
testcase_17 AC 32 ms
11,008 KB
testcase_18 AC 32 ms
11,008 KB
testcase_19 AC 32 ms
11,008 KB
testcase_20 AC 32 ms
11,136 KB
testcase_21 AC 34 ms
11,008 KB
testcase_22 AC 33 ms
11,008 KB
testcase_23 AC 32 ms
11,008 KB
testcase_24 AC 33 ms
11,008 KB
testcase_25 AC 33 ms
11,008 KB
testcase_26 AC 33 ms
11,008 KB
testcase_27 AC 32 ms
11,008 KB
testcase_28 AC 32 ms
11,008 KB
testcase_29 AC 33 ms
11,008 KB
testcase_30 AC 32 ms
10,880 KB
testcase_31 AC 33 ms
11,136 KB
testcase_32 AC 32 ms
11,008 KB
testcase_33 AC 31 ms
11,008 KB
testcase_34 AC 32 ms
11,008 KB
testcase_35 AC 32 ms
11,136 KB
testcase_36 AC 32 ms
11,008 KB
testcase_37 AC 32 ms
11,136 KB
testcase_38 AC 32 ms
11,136 KB
testcase_39 AC 32 ms
10,880 KB
testcase_40 AC 32 ms
10,880 KB
testcase_41 AC 31 ms
11,008 KB
testcase_42 AC 262 ms
11,264 KB
testcase_43 AC 264 ms
11,136 KB
testcase_44 AC 261 ms
11,136 KB
testcase_45 AC 260 ms
11,136 KB
testcase_46 AC 260 ms
11,136 KB
testcase_47 AC 264 ms
11,136 KB
testcase_48 AC 264 ms
11,136 KB
testcase_49 AC 261 ms
11,264 KB
testcase_50 AC 263 ms
11,136 KB
testcase_51 AC 269 ms
11,136 KB
testcase_52 AC 262 ms
11,136 KB
testcase_53 AC 262 ms
11,136 KB
testcase_54 AC 260 ms
11,136 KB
testcase_55 AC 259 ms
11,136 KB
testcase_56 AC 267 ms
11,136 KB
testcase_57 AC 338 ms
36,464 KB
testcase_58 AC 392 ms
34,756 KB
testcase_59 AC 338 ms
36,724 KB
testcase_60 AC 494 ms
25,336 KB
testcase_61 AC 452 ms
28,656 KB
testcase_62 AC 469 ms
27,632 KB
testcase_63 AC 389 ms
34,800 KB
testcase_64 AC 278 ms
42,432 KB
testcase_65 AC 312 ms
37,488 KB
testcase_66 AC 434 ms
32,756 KB
testcase_67 AC 519 ms
25,720 KB
testcase_68 AC 457 ms
28,144 KB
testcase_69 AC 386 ms
34,548 KB
testcase_70 AC 372 ms
35,292 KB
testcase_71 AC 415 ms
33,420 KB
testcase_72 AC 304 ms
37,620 KB
testcase_73 AC 434 ms
29,428 KB
testcase_74 AC 270 ms
42,500 KB
testcase_75 AC 307 ms
42,872 KB
testcase_76 AC 381 ms
35,016 KB
testcase_77 AC 487 ms
32,504 KB
testcase_78 AC 456 ms
33,748 KB
testcase_79 AC 390 ms
39,436 KB
testcase_80 AC 515 ms
30,676 KB
testcase_81 AC 511 ms
30,412 KB
testcase_82 AC 438 ms
37,864 KB
testcase_83 AC 515 ms
31,036 KB
testcase_84 AC 504 ms
31,964 KB
testcase_85 AC 338 ms
40,620 KB
testcase_86 AC 292 ms
45,752 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