結果

問題 No.1950 片道きゃっちぼーる
ユーザー rin204rin204
提出日時 2022-05-20 22:05:35
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,765 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 81,780 KB
実行使用メモリ 168,836 KB
最終ジャッジ日時 2023-10-20 12:46:24
合計ジャッジ時間 9,201 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,512 KB
testcase_01 AC 37 ms
53,512 KB
testcase_02 AC 37 ms
53,512 KB
testcase_03 AC 233 ms
164,820 KB
testcase_04 AC 232 ms
165,936 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 198 ms
149,672 KB
testcase_07 AC 208 ms
168,836 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 241 ms
168,388 KB
testcase_12 AC 246 ms
168,380 KB
testcase_13 AC 238 ms
143,660 KB
testcase_14 AC 225 ms
143,464 KB
testcase_15 WA -
testcase_16 AC 218 ms
144,872 KB
testcase_17 WA -
testcase_18 AC 251 ms
166,660 KB
testcase_19 WA -
testcase_20 AC 223 ms
168,724 KB
testcase_21 AC 246 ms
164,860 KB
testcase_22 AC 261 ms
164,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            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
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x
        self.max_[x] = max(self.max_[x], self.max_[y])

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, 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 self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

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

n = int(input())
X = list(map(int, input().split()))
A = list(map(int, input().split()))
dic = {}
for i, x in enumerate(X):
    dic[x] = i
UF = UnionFind(n)

for i in range(n):
    UF.max_[i] = A[i] + X[i]

for i in range(n):
    if X[i] - A[i] in dic:
        UF.union(i, dic[X[i] - A[i]])
    if X[i] + A[i] in dic:
        UF.union(i, dic[X[i] + A[i]])

for i in range(n):
    print(UF.max_[UF.find(i)] - X[i])







0