結果
| 問題 |
No.277 根掘り葉掘り
|
| コンテスト | |
| ユーザー |
rlangevin
|
| 提出日時 | 2023-07-31 22:50:39 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,038 bytes |
| コンパイル時間 | 330 ms |
| コンパイル使用メモリ | 82,356 KB |
| 実行使用メモリ | 99,924 KB |
| 最終ジャッジ日時 | 2024-10-10 15:39:05 |
| 合計ジャッジ時間 | 4,782 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 10 WA * 8 |
ソースコード
import sys
input = sys.stdin.readline
def non_rec_dfs(s, G):
N = len(G)
stack = []
stack.append(s)
par = [-1] * N
depth = [-1] * N
shallow = [10**18] * N
sz = [1] * N
depth[s] = 0
while stack:
u = stack.pop()
if u >= 0:
stack.append(~u)
for v in G[u]:
if v == par[u]:
continue
par[v] = u
depth[v] = depth[u] + 1
stack.append(v)
else:
u = ~u
if len(G[u]) == 1 and u != 0:
shallow[u] = 0
if u != 0:
sz[par[u]] += sz[u]
shallow[par[u]] = min(shallow[par[u]], shallow[u] + 1)
return depth, shallow
N = int(input())
G = [[] for i in range(N)]
for i in range(N - 1):
u, v = map(int, input().split())
u, v = u - 1, v - 1
G[u].append(v)
G[v].append(u)
ans1, ans2 = non_rec_dfs(0, G)
for a, b in zip(ans1, ans2):
print(min(a, b))
rlangevin