結果

問題 No.1950 片道きゃっちぼーる
ユーザー rlangevin
提出日時 2023-06-05 22:43:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 882 ms / 3,000 ms
コード長 810 bytes
コンパイル時間 374 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 170,064 KB
最終ジャッジ日時 2024-12-29 06:29:05
合計ジャッジ時間 11,852 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
def bfs(G, s, N):
    Q = deque([s])
    while Q:
        u = Q.popleft()
        for v in G[u]:
            if seen[v] != -1:
                continue
            seen[v] = seen[u]
            Q.append(v)

    return seen

N = int(input())
X = list(map(int, input().split()))
A = list(map(int, input().split()))

G = [[] for i in range(N)]
D = defaultdict(int)
for i in range(N):
    D[X[i]] = i
    
for i in range(N):
    if X[i] + A[i] in D:
        G[D[X[i] + A[i]]].append(i)
    if X[i] - A[i] in D:
        G[D[X[i] - A[i]]].append(i)
        
seen = [-1] * N
L = []
for i in range(N):
    L.append((X[i] + A[i], i))
L.sort(reverse=True)

for d, i in L:
    if seen[i] != -1:
        continue
    seen[i] = d
    bfs(G, i, N)
    
for i in range(N):
    print(seen[i] - X[i])
0