Submission #22967713


Source Code Expand

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            # 積極的aggregation
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        # 同じノード同士ならばなにもしない
        if x == y:
            return

        # 既知の親子で小さいものが左に来るべき
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        # rootノードを負の値で参照量をカウントしたいため、このような+=が入っている
        self.parents[x] += self.parents[y]
        # rootノードでなければ、正のindex値を入れる
        self.parents[y] = x

    def size(self, x):
        # rootノードの参照料を保存したものを取り出している
        return -self.parents[self.find(x)]

N,M=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
import collections
# import numpy as np

uf = UnionFind(N)
for _ in range(M):
    c,d = map(int,input().split())
    c-=1; d-=1
    uf.union(c,d)
    
import itertools
import statistics
cls = [(uf.find(n), n) for n in range(N)]
cls.sort()
cls = {k:list(map(lambda x:x[1], v)) for k, v in itertools.groupby(cls, key=lambda x:x[0])}
nums = set(cls.keys())

checks = []
for num in nums:
    a = statistics.mean([A[ai] for ai in cls[num]])
    b = statistics.mean([B[ai] for ai in cls[num]])
    checks.append( abs(a - b)  < 1/(10**6))
if all(checks):
    print("Yes")
else:
    print("No")

Submission Info

Submission Time
Task B - Values
User lightning
Language PyPy3 (7.3.0)
Score 400
Code Size 1772 Byte
Status AC
Exec Time 1070 ms
Memory 171436 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 44
Set Name Test Cases
Sample 00_sample_00, 00_sample_01, 00_sample_02, 00_sample_03
All 00_sample_00, 00_sample_01, 00_sample_02, 00_sample_03, 01_small_0, 01_small_1, 01_small_2, 01_small_3, 01_small_4, 01_small_5, 01_small_6, 01_small_7, 01_small_8, 01_small_9, 02_large_0, 02_large_1, 02_largecon_0, 02_largecon_1, 02_largecon_2, 02_largecon_3, 02_largecon_4, 02_largecon_5, 02_toolarge_0, 02_toolarge_1, 02_toolarge_2, 02_toolarge_3, 03_few_e_0, 03_few_e_1, 03_few_e_2, 03_few_e_3, 03_few_e_4, 03_few_e_5, 03_is_tree_0, 03_is_tree_1, 03_is_tree_2, 03_is_tree_3, 04_kill_overflow_0, 04_kill_overflow_1, 04_kill_overflow_2, 04_kill_overflow_3, 04_sumequal_0, 04_sumequal_1, 04_sumequal_2, 04_sumequal_3
Case Name Status Exec Time Memory
00_sample_00 AC 157 ms 72684 KiB
00_sample_01 AC 90 ms 72604 KiB
00_sample_02 AC 90 ms 72660 KiB
00_sample_03 AC 89 ms 72568 KiB
01_small_0 AC 92 ms 72680 KiB
01_small_1 AC 93 ms 72724 KiB
01_small_2 AC 93 ms 72500 KiB
01_small_3 AC 89 ms 72668 KiB
01_small_4 AC 91 ms 72524 KiB
01_small_5 AC 94 ms 72680 KiB
01_small_6 AC 91 ms 72564 KiB
01_small_7 AC 91 ms 72568 KiB
01_small_8 AC 94 ms 72680 KiB
01_small_9 AC 90 ms 72668 KiB
02_large_0 AC 847 ms 128004 KiB
02_large_1 AC 479 ms 96028 KiB
02_largecon_0 AC 538 ms 125492 KiB
02_largecon_1 AC 538 ms 137872 KiB
02_largecon_2 AC 549 ms 124992 KiB
02_largecon_3 AC 460 ms 118388 KiB
02_largecon_4 AC 569 ms 147872 KiB
02_largecon_5 AC 539 ms 126152 KiB
02_toolarge_0 AC 1000 ms 141432 KiB
02_toolarge_1 AC 1019 ms 152600 KiB
02_toolarge_2 AC 1070 ms 150572 KiB
02_toolarge_3 AC 996 ms 141572 KiB
03_few_e_0 AC 608 ms 137800 KiB
03_few_e_1 AC 798 ms 167088 KiB
03_few_e_2 AC 686 ms 147188 KiB
03_few_e_3 AC 760 ms 166712 KiB
03_few_e_4 AC 843 ms 171436 KiB
03_few_e_5 AC 730 ms 154612 KiB
03_is_tree_0 AC 760 ms 122244 KiB
03_is_tree_1 AC 701 ms 119772 KiB
03_is_tree_2 AC 687 ms 116508 KiB
03_is_tree_3 AC 705 ms 116856 KiB
04_kill_overflow_0 AC 846 ms 132340 KiB
04_kill_overflow_1 AC 658 ms 114940 KiB
04_kill_overflow_2 AC 691 ms 113088 KiB
04_kill_overflow_3 AC 660 ms 114076 KiB
04_sumequal_0 AC 782 ms 128144 KiB
04_sumequal_1 AC 515 ms 95900 KiB
04_sumequal_2 AC 435 ms 93640 KiB
04_sumequal_3 AC 572 ms 101700 KiB