← 返回 sofi 的题目列表Graph Variant: Endings Reachable Only If Both Options Are Explored (Follow-up)
类型:online_judge
Follow-up to the previous problem: model everything as a directed graph (nodes are story segments; edges are option transitions). Add a constraint:
For a node with choice = [start, op1, op2], an ending is considered "validly reachable" from start only if both branches (the path starting with choosing op1 and the path starting with choosing op2) can reach the same ending (possibly via different routes).
Design an algorithm that, given startNode and endingList, returns all endings satisfying this “both branches can reach it” condition.
Requirements:
Explain how you handle cycles.
Provide time complexity and when the approach is suitable.
Note: if you interpret the follow-up differently, state your precise definition first.
Example
Input
choices=[[1,"A","B"],[2,"C","D"],[3,"E","F"]]
options={"A":2,"B":3,"C":4,"D":5,"E":4,"F":7}
endingList=[4,5,7]
startNode=1
Output
[4]