Approach
Breadth-first search
For ABC315 E — Prerequisites, 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
- 130 lines of Python from the credited upstream file abc315_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 deque5from typing import List, Tuple6 7 8class TopologicalSorting:9 """10 See:11 https:atcoder.jp/contests/abc291/submissions/3924105512 """13 14 def __init__(self, vertex_count: int) -> None:15 self.vertex_count = vertex_count16 self.graph = [[] for _ in range(vertex_count)]17 self.indegrees = [0] * vertex_count18 19 def add_edge(self, frm: int, to: int) -> None:20 """21 Args:22 frm(from) -> to: Vertex number (0-indexed).23 """24 assert 0 <= frm < self.vertex_count25 assert 0 <= to < self.vertex_count26 27 self.graph[frm].append(to)28 self.indegrees[to] += 129 30 def sort(self) -> Tuple[bool, List[int]]:31 """32 Returns:33 is_DAG: Is it DAG (Directed Acyclic Graph) ?34 orders: Order of vertices (0-indexed).35 """36 que = deque([i for i in range(self.vertex_count) if self.indegrees[i] == 0])37 results = list()38 39 40 41 if len(que) == 0:42 return False, []43 44 while que:45 46 47 48 49 vertex = que.popleft()50 results.append(vertex)51 52 for to in self.graph[vertex]:53 self.indegrees[to] -= 154 55 56 57 58 if self.indegrees[to] == 0:59 que.append(to)60 61 if len(results) == self.vertex_count:62 return True, results63 64 else:65 return False, []66 67 def sort_only_reachable_vertices_from(68 self, start_id: int = 069 ) -> Tuple[bool, List[int]]:70 """71 Returns:72 is_DAG: Is it DAG (Directed Acyclic Graph) ?73 orders: Order of vertices (1-indexed).74 """75 is_DAG, orders = self.sort()76 77 if not is_DAG:78 return is_DAG, orders79 80 que = deque([start_id])81 visited = [False] * self.vertex_count82 83 while que:84 cur = que.popleft()85 86 if visited[cur]:87 continue88 89 visited[cur] = True90 91 for to in self.graph[cur]:92 if visited[to]:93 continue94 95 que.append(to)96 97 reachable_orders = list()98 99 for order in orders:100 if visited[order]:101 reachable_orders.append(order + 1)102 103 return is_DAG, reachable_orders104 105 106def main():107 import sys108 109 input = sys.stdin.readline110 111 n = int(input())112 ts = TopologicalSorting(n)113 114 for i in range(n):115 ci, *pi = map(int, input().split())116 117 if ci == 0:118 continue119 120 for pij in pi:121 pij -= 1122 ts.add_edge(frm=i, to=pij)123 124 is_DAG, orders = ts.sort_only_reachable_vertices_from(start_id=0)125 print(*orders[1:][::-1])126 127 128if __name__ == "__main__":129 main()130