結果

問題 No.1950 片道きゃっちぼーる
ユーザー 👑 rin204
提出日時 2022-05-20 22:09:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 570 ms / 3,000 ms
コード長 2,192 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 81,896 KB
実行使用メモリ 202,464 KB
最終ジャッジ日時 2024-09-20 08:19:51
合計ジャッジ時間 10,785 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

def scc(N, G):
    order = []
    used = [False]*N
    group = [None]*N
    RG = [[] for _ in range(N)]
    for i in range(N):
        for j in G[i]:
            RG[j].append(i)
    
    def dfs(pos):
        stack = [(1, pos), (0, pos)]
        while stack:
            t, pos = stack.pop()
            if t == 0:
                if used[pos]:
                    stack.pop()
                    continue
                used[pos] = True
                for npos in G[pos]:
                    if not used[npos]:
                        stack.append((1, npos))
                        stack.append((0, npos))
            else:
                order.append(pos)

    def rdfs(pos, col):
        stack = [pos]
        group[pos] = col
        used[pos] = True
        while stack:
            pos = stack.pop()
            for npos in RG[pos]:
                if not used[npos]:
                    used[npos] = True
                    group[npos] = col
                    stack.append(npos)
                    
    for i in range(N):
        if not used[i]:
            dfs(i)
    used = [False]*N
    label = 0
    for s in reversed(order):
        if not used[s]:
            rdfs(s, label)
            label += 1
    return label, group

def construct(N, G, label, group):
    G0 = [[] for _ in range(label)]
    GP = [[] for _ in range(label)]
    for v in range(N):
        lbs = group[v]
        for w in G[v]:
            lbt = group[w]
            if lbs == lbt:
                continue
            G0[lbs].append(lbt)
        GP[lbs].append(v)
    return G0, GP

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
edges = [[] for _ in range(n)]
for i in range(n):
    if X[i] - A[i] in dic:
        edges[i].append(dic[X[i] - A[i]])
    if X[i] + A[i] in dic:
        edges[i].append(dic[X[i] + A[i]])

label, group = scc(n, edges)
g0, gp = construct(n, edges, label, group)
dp = [0] * label
for i in range(label - 1, -1, -1):
    for j in gp[i]:
        dp[i] = max(dp[i], A[j] + X[j])
    for j in g0[i]:
        dp[i] = max(dp[i], dp[j])

for i in range(n):
    print(dp[group[i]] - X[i])
0