結果

問題 No.1950 片道きゃっちぼーる
ユーザー 👑 rin204rin204
提出日時 2022-05-20 22:09:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 608 ms / 3,000 ms
コード長 2,192 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 201,872 KB
最終ジャッジ日時 2023-10-20 12:51:05
合計ジャッジ時間 11,210 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,452 KB
testcase_01 AC 38 ms
53,452 KB
testcase_02 AC 37 ms
53,452 KB
testcase_03 AC 456 ms
201,872 KB
testcase_04 AC 462 ms
201,136 KB
testcase_05 AC 37 ms
53,420 KB
testcase_06 AC 336 ms
174,100 KB
testcase_07 AC 344 ms
167,616 KB
testcase_08 AC 362 ms
167,288 KB
testcase_09 AC 370 ms
172,176 KB
testcase_10 AC 434 ms
186,604 KB
testcase_11 AC 426 ms
186,836 KB
testcase_12 AC 435 ms
186,908 KB
testcase_13 AC 519 ms
177,852 KB
testcase_14 AC 392 ms
150,072 KB
testcase_15 AC 608 ms
168,968 KB
testcase_16 AC 340 ms
157,580 KB
testcase_17 AC 37 ms
53,420 KB
testcase_18 AC 432 ms
177,488 KB
testcase_19 AC 574 ms
167,600 KB
testcase_20 AC 346 ms
167,628 KB
testcase_21 AC 390 ms
169,368 KB
testcase_22 AC 387 ms
169,360 KB
権限があれば一括ダウンロードができます

ソースコード

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