結果
問題 | No.277 根掘り葉掘り |
ユーザー | 双六 |
提出日時 | 2020-08-04 02:24:49 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 483 ms / 3,000 ms |
コード長 | 1,753 bytes |
コンパイル時間 | 215 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 184,572 KB |
最終ジャッジ日時 | 2024-09-13 20:33:27 |
合計ジャッジ時間 | 6,999 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
54,400 KB |
testcase_01 | AC | 46 ms
54,016 KB |
testcase_02 | AC | 45 ms
54,016 KB |
testcase_03 | AC | 48 ms
54,656 KB |
testcase_04 | AC | 47 ms
54,656 KB |
testcase_05 | AC | 47 ms
54,784 KB |
testcase_06 | AC | 47 ms
54,528 KB |
testcase_07 | AC | 47 ms
54,784 KB |
testcase_08 | AC | 46 ms
53,760 KB |
testcase_09 | AC | 451 ms
184,572 KB |
testcase_10 | AC | 226 ms
100,280 KB |
testcase_11 | AC | 341 ms
100,352 KB |
testcase_12 | AC | 439 ms
142,464 KB |
testcase_13 | AC | 414 ms
98,432 KB |
testcase_14 | AC | 345 ms
102,272 KB |
testcase_15 | AC | 483 ms
111,616 KB |
testcase_16 | AC | 351 ms
105,984 KB |
testcase_17 | AC | 337 ms
102,724 KB |
testcase_18 | AC | 329 ms
101,120 KB |
testcase_19 | AC | 348 ms
102,272 KB |
ソースコード
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict from collections import deque con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class Graph(object): def __init__(self): self.graph = defaultdict(list) def __len__(self): return len(self.graph) def add_edge(self, a, b): self.graph[a].append(b) class BFS(object): def __init__(self, graph, s, leafDist, N): self.g = graph.graph self.Q = deque(); self.Q.append(s) self.dist = [INF] * N; self.dist[s] = 0 while self.Q: v = self.Q.popleft() jud = 0 for i in self.g[v]: if self.dist[i] == INF: self.dist[i] = self.dist[v] + 1 self.Q.append(i) jud = 1 if jud == 0: leafDist[v] = 0 class BFS2(object): def __init__(self, graph, s, leafDist, N): self.g = graph.graph self.Q = deque(); self.Q.append(s) self.dist = [INF] * N; self.dist[s] = 0 while self.Q: v = self.Q.popleft() for i in self.g[v]: if self.dist[i] == INF: self.dist[i] = self.dist[v] + 1 leafDist[i] = min(leafDist[i], leafDist[v] + 1) self.Q.append(i) def DFS(G, leafDist, visit, node): for i in G.graph[node]: if visit[i] != 1: visit[i] = 1 DFS(G, leafDist, visit, i) leafDist[node] = min(leafDist[i] + 1, leafDist[node]) #処理内容 def main(): N = int(input()) G = Graph() for i in range(N - 1): a, b = getlist() a -= 1; b -= 1 G.add_edge(a, b) G.add_edge(b, a) leafDist = [INF] * N BF = BFS(G, 0, leafDist, N) visit = [0] * N visit[0] = 1 DFS(G, leafDist, visit, 0) # print(leafDist) BF2 = BFS2(G, 0, leafDist, N) for i in range(N): print(min(BF.dist[i], leafDist[i])) if __name__ == '__main__': main()