← 返回 nvidia 的题目列表Graph API: Insert/Configure/Validate with Cycle Detection and Structural Constraints
类型:online_judge
Design and implement a directed Graph component to model nodes with dependencies. Implement:
insertNode(id, ...)
addDependency(u, v): u depends on v (must execute v before u).
configureGraph(config): initialize/configure the graph (e.g., batch add edges / set node properties).
validate(requirements): check whether the graph satisfies the requirements; return true/false or a list of validation errors.
validate must cover
Cycle detection: the graph must be acyclic.
Dependency order: a valid topological order must exist (or validate a proposed order).
Structural constraints: additional constraints provided in the interview, e.g.:
some node types must appear after others
in-degree/out-degree limits
a path from a source to a sink must exist
Constraints
N ≤ 1e5, M ≤ 2e5.
Sample scenarios
0<-1, 1<-2 is valid and acyclic.
0<-1, 1<-0 must report a cycle.
Additional constraint: node 0 must appear after node 2; if impossible, validate fails.
Example
Input
3 2
0 1
1 2
Output
valid