Approach
Breadth-first search
For ABC428 E — Farthest Vertex, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 55 lines of Python from the credited upstream file abc428_e.py.
- The implementation visibly relies on sequence storage, ordered lookup, work queue.
- No explicit loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4from collections import deque5 6 7def calc_depth(vertex_count: int, graph, source: int):8 PENDING = -19 depth = [PENDING for _ in range(vertex_count)]10 depth[source] = 011 d = deque([source])12 13 while d:14 vertex = d.popleft()15 16 for g in graph[vertex]:17 if depth[g] == PENDING:18 depth[g] = depth[vertex] + 119 d.append(g)20 21 return depth22 23 24def main():25 import sys26 27 input = sys.stdin.readline28 29 n = int(input())30 graph = [[] for _ in range(n)]31 32 for _ in range(n - 1):33 ai, bi = map(int, input().split())34 ai -= 135 bi -= 136 37 graph[ai].append(bi)38 graph[bi].append(ai)39 40 41 42 dist_0 = calc_depth(n, graph, 0)43 s = max([(dist_0[i], i) for i in range(n)])[1]44 dist_s = calc_depth(n, graph, s)45 t = max([(dist_s[i], i) for i in range(n)])[1]46 dist_t = calc_depth(n, graph, t)47 48 for i in range(n):49 ans = max((dist_s[i], s), (dist_t[i], t))[1] + 150 print(ans)51 52 53if __name__ == "__main__":54 main()55