1109. Count Hamiltonian Paths in DAG (Bitmask DP)

HardGraphsBitmaskDynamic ProgrammingGraph

Given a directed acyclic graph with n nodes and edges, count the number of Hamiltonian paths — directed paths that visit every node exactly once following the edges. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Integer — the number of Hamiltonian paths.

Examples

Example 1
Input: {"n":3,"edges":[[0,1],[1,2],[0,2]]}
Output: 1
Explanation: Only 0->1->2 covers all nodes.
Example 2
Input: {"n":3,"edges":[[0,1],[0,2]]}
Output: 0
Explanation: No single path covers all.
Example 3
Input: {"n":1,"edges":[]}
Output: 1
Explanation: The single node is a path.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →