Skip to content

ORIGINAL CODING QUESTION

Count valid dependency orders

Count vertex permutations in which every directed edge u→v places u before v. Cyclic inputs have zero orders.

Open practice workspace

Original exercise · hard. Editorial role relevance does not imply a company has asked this question.

Input and output

n m, then m vertex pairs (u,v). Vertices are zero-based.

Constraints

1 ≤ n ≤ 10; 0 ≤ m ≤ 30; no repeated edges; vertex IDs in [0,n). The count fits signed 32-bit.

Example 1

Input

3 2 0 2 1 2

Output

2

Vertices 0 and 1 can swap order; both precede 2.

Example 2

Input

2 2 0 1 1 0

Output

0

The cycle has no valid order.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

A placed-vertex set determines which vertices are available.

Hint 2

Each candidate’s prerequisites must all be placed.

Hint 3

Memoize counts by the placed bitmask.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →