Do a topological sort of the DAG, then scan the vertices from the target backwards to the source. For each vertex v
, keep a count of the number of paths from v
to the target. When you get to the source, the value of that count is the answer. That is O(V+E)
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…