We are asked: given a simple graph with n ≤ 19, m edges, count the number of simple cycles. A simple cycle is a cycle (closed walk) with no repeated vertices or edges. The cycles are considered as sets of edges? Usually counting simple cycles in an undirected graph: each cycle is a sequence of vertices (v1,v2,...,vk,v1) with k>=3, no repetitions, and edges exist between consecutive vertices and vk to v1. Usually cycles are considered up to cyclic rotation and reversal? In undirected graphs, each cycle can be represented in two directions, and starting point can be any vertex. Usually we count each cycle exactly once, disregarding ordering; i.e., each set of edges that form a cycle is considered one cycle. Typically, in such problems, cycles are considered distinct if they are not the same set of edges. However, the problem statement says "simple cycle is a cycle with no repeated vertices or edges." And they output 7 for K4. K4 has 4 triangles and 3 squares? Actually K4 has 4 triangles (each combination of 3 vertices) and 3 distinct 4-cycles? Let's check: In K4 (complete graph on 4 vertices), cycles of length 4: a Hamiltonian cycle on 4 vertices. There are (4-1)!/2 = 3 distinct undirected cycles (since number of distinct Hamiltonian cycles in complete graph is (n-1)!/2). So total cycles: 4 triangles + 3 squares = 7. So indeed they count each cycle as a set of edges (or the cycle itself) up to direction and starting point. So we need to count all simple cycles of length >=3 in the graph.
Constraints n ≤ 19, so we can use DP over subsets, or inclusion-exclusion, or backtracking. Since m can be up to n choose 2 ~ 171. Counting all simple cycles in an undirected graph is #P-hard in general, but for n=19 we can use exponential algorithm.
One typical approach: For each vertex v, consider cycles where v is the smallest vertex (or some canonical root) to avoid overcount. For each subset S of vertices that includes v and another vertex u, we can count number of paths from v to u using exactly the vertices in S (or a subset). Then cycles are when there is an edge between u and v, and we add that edge to the path to form a cycle.
Standard DP: For each subset mask and current endpoint w, compute number of simple paths from a fixed root (lowest vertex) to w that use exactly the vertices in mask and start at root. We can compute DP[mask][w] = sum over neighbors x in mask{w} of DP[mask without w][x], with base DP[1<<root][root] = 1. Then for each mask that contains root and at least two other vertices, and for each w in mask where w is neighbor of root, we can add DP[mask][w] to cycle count if edge (root,w) exists. But careful: This counts each cycle multiple times? Let's analyze: For a cycle, we can choose the smallest vertex as root. For a cycle of length L, the smallest vertex (by index) will be unique (if we choose smallest label as root). The DP counts simple paths from root to w using exactly vertices in mask, with root the smallest? Actually if we fix root as the smallest vertex in the cycle, then any cycle containing root will be counted exactly once? But we must ensure that we don't count cycles that don't have root as smallest? We will iterate root from 0 to n-1? Typically approach: For each vertex v in increasing order, consider only cycles where v is the smallest vertex (by index) in the cycle. Then we run DP with v as root, counting paths from v to other vertices using subsets that have v as smallest? Actually we can restrict DP to subsets that contain v and only vertices > v (or at least not smaller than v) to avoid double counting. Then for each w > v that is neighbor of v, and for each subset containing v and w and maybe other vertices all > v (or at least not less than v), the number of paths from v to w using exactly those vertices is DP[mask][w]. Then each such path plus the edge (v,w) forms a cycle. And since v is the smallest vertex in that cycle, each cycle is counted exactly once (since there is a unique smallest vertex). That is a typical method to count undirected simple cycles.
But caution: For an undirected graph, each path from v to w counted in DP is a simple path (no repeated vertices). However, because the graph is undirected, the DP when building paths might count each path twice if we consider both directions? Let's see: DP[mask][w] counts number of walks from v to w that use exactly vertices in mask, but note that for an undirected edge, moving from x to w is considered. Since we start at v and extend paths by adding neighbors, each simple path is counted exactly once because the direction is determined: from v to w. Since v is fixed start, there's no double counting due to reversal. So each cycle (with smallest vertex v) will correspond to exactly one path from v to some neighbor w, where v < w (to avoid counting same cycle twice when v is smallest, we also need to ensure that w > v? Actually the cycle has smallest vertex v. The two neighbors of v on the cycle are some vertices (say a and b). In the cycle, there are two paths from v to some other vertex? But our method: we consider each neighbor w of v, and count paths from v to w that use a set of vertices (all > v) and then close the cycle with edge (v,w). However, for a given cycle, there are two v-w paths along the cycle: one going one direction, the other the opposite direction. Both would produce cycles if we consider w as each of the two neighbors? But we restrict w > v? Let's examine: Suppose cycle vertices: v (smallest), and others: a, b, c, ... . For each neighbor w of v on the cycle, there is a unique path from v to w that goes around the cycle the other way (the complement path includes v's other neighbor). However, if we only consider w > v, then exactly one of the two neighbors might be > v? Possibly both neighbors could be > v. Then we might count the cycle twice? Let's test with a triangle: vertices 1,2,3. Suppose v=1 is smallest. Neighbors of v are 2 and 3, both >1. The cycle (1-2-3-1) can be obtained by path from 1 to 2: 1-2, then edge (2,1) to close? Actually we consider path from v to w and then add edge (w,v). For triangle, if we take w=2, the path from 1 to 2 is just the edge (1,2). Then adding edge (2,1) would close, but that's just the edge (1,2) used twice? Actually careful: We count a cycle when there is a path from v to w using vertices in mask (which includes v and w and possibly others) and edge (v,w) exists. The cycle is formed by the path plus that edge. For triangle (1-2-3-1), possible paths from v=1 to w=2 that use vertices {1,2,3}? The path 1-3-2 (1->3->2) uses vertices 1,3,2. That's a path from 1 to 2. Adding edge (2,1) gives cycle 1-3-2-1. That's same cycle as (1-2-3-1)? Actually it's the same cycle (just traversed reverse). The path 1-2 (direct edge) plus edge (1,2) would give a 2-cycle? That's not a simple cycle because edge repeated. Actually we need the path to not include the edge (v,w) already; we consider simple paths that don't use edge (v,w) except at the end? The DP counts paths that may include edge (v,w)? But the path itself shouldn't use edge (v,w) because then adding it would duplicate edge. However, DP may include a path that goes directly from v to w as a single edge. But then adding edge (v,w) would create a cycle of length 2, which is not allowed because it's not simple (it repeats edge, and also length 2 requires two vertices, which is not a simple cycle because need at least 3 distinct vertices). Actually a simple cycle in an undirected graph must have at least 3 vertices. So we should exclude paths that consist of a single edge (i.e., mask = {v,w} and DP[mask][w] = 1 if edge exists). So we need to count cycles of length >=3 only. The DP for mask size >=3 ensures cycles of length at least 3.
Now back to double counting: In the triangle, both w=2 and w=3 can be used:
- For w=2, path 1-3-2 (mask {1,2,3}) gives cycle.
- For w=3, path 1-2-3 (mask {1,2,3}) gives cycle.
Thus the cycle would be counted twice if we count all neighbors w regardless. To avoid double counting, we enforce that w is the largest vertex in the cycle? Or we enforce that w is the smallest neighbor? Standard trick: For each cycle, pick the smallest vertex v, and then pick the neighbor w of v that is the smallest among the two neighbors? Hmm typical method for counting cycles in undirected graphs using DP is to only consider cycles where the root is the smallest vertex and the other endpoint (w) is larger than v and also we restrict that the root's two neighbors are considered in some order? Actually many solutions for counting Hamiltonian cycles use a similar DP, but for counting all simple cycles they often use root as smallest and then sum over subsets that contain root and some other vertex u, and then for each u neighbor of root, add DP[mask][u] to answer, but they do not restrict u > root; they just sum over all u neighbor of root and later divide by 2? Let's think: In the literature, counting simple cycles in undirected graphs with n up to 20 is often done using DP over subsets and root as smallest, but they count each cycle exactly once by ensuring that the root is the smallest and the second vertex in the cycle (or the neighbor used to close) is larger than the last vertex? I recall a common approach: For each cycle, assign the smallest vertex as start, and also the second vertex in the cycle (in some order) is less than the last vertex? Actually there is known algorithm by Björklund, Husfeldt, Kaski, Koivisto for counting cycles, but simpler is to use inclusion-exclusion or backtracking.
But with n ≤ 19, we can also use brute force enumeration of all subsets and test if it's a cycle? For each subset of vertices, we can check if it induces a simple cycle (i.e., all vertices in the subset have degree 2 within the induced subgraph, and the subgraph is connected). However, this counts cycles as vertex sets, but a cycle of length k corresponds to exactly 2k possible vertex sequences? Actually a cycle as a subgraph (set of edges) is determined by a set of vertices of size k, but a given set of k vertices can contain multiple cycles? For example, K4 has triangles: each triangle is a set of 3 vertices. But also a set of 4 vertices can contain multiple cycles: For a 4-vertex set that is a clique, there are 3 different 4-cycles (each a Hamiltonian cycle on those 4 vertices). So cycles are not uniquely defined by vertex set. So subset enumeration must consider edge sets.
Alternative: Use DP counting each cycle as a closed walk with a designated start and direction? But then we would overcount. Could divide by 2*L if we count ordered cycles. But that's messy.
Standard method: For each vertex v, run DP over subsets containing v and vertices with index > v only. Count number of simple paths from v to each u > v. Then for each u > v, if edge (v,u) exists, add DP[mask][u] to answer. This will count each cycle exactly once. Let's verify with triangle.
Suppose vertices: 1 (smallest), 2, 3. Run DP with root=1, and restrict to vertices with index >= root? Actually we want to only include vertices whose index >= root? If we restrict to vertices > root, then only vertices 2 and 3 are allowed. The triangle includes vertices {1,2,3}, all allowed because 2 and 3 > 1. DP will count paths from 1 to u using subsets that include 1 and u and possibly other vertices >1.
For u=2, path from 1 to 2 using vertices {1,2,3}: The possible simple paths: 1-3-2. DP will count that as 1 (if edge 1-3 and 3-2 exist). Since edge (1,2) exists, we add DP[mask][2] = 1, forming cycle (1-3-2-1). That is a triangle.
For u=3, path from 1 to 3 using vertices {1,2,3}: possible path 1-2-3. DP counts that as 1. Since edge (1,3) exists, add DP[mask][3] = 1, forming cycle (1-2-3-1). That's also the same triangle? Wait are these two cycles the same? The triangle (1,2,3) as a set of edges includes edges (1,2), (2,3), (3,1). The cycle (1-3-2-1) uses edges (1,3), (3,2), (2,1) which is exactly the same set of edges. So both represent the same cycle. Thus they are duplicates. So counting both would overcount by factor 2 for triangle.
If we restrict u > root, both u=2 and u=3 are > root. So double count.
Thus we need an additional restriction. Typically, to count each cycle exactly once, we can enforce that the smallest vertex is v, and also the largest vertex (or some other) is something. Actually many known solutions: For counting simple cycles in an undirected graph, one method is: For each vertex v, we consider cycles where v is the smallest vertex. Then we run DP over subsets S that contain v and only vertices > v. For each vertex u in S, u != v, we compute number of paths from v to u using vertices in S. Then for each neighbor w of v such that w > v, we add DP[S][w] to answer only if S contains w and also something? Wait, that still double counts.
Another common trick: For each cycle, there are two possible v-to-u paths that close the cycle: one going clockwise, one counterclockwise. To count each cycle only once, we can enforce that the neighbor u (the endpoint) is the smallest among the neighbors of v on the cycle? Or more simply, we can enforce that after we have chosen v as smallest, we also enforce that the second vertex in the cycle (the vertex after v when traversing the cycle in some canonical direction) is less than the last vertex (the vertex before v). But we can't easily encode that in DP.
Alternate approach: Count each cycle twice (once for each direction and once for each choice of start) and then divide by 2L? That becomes complicated because L varies.
Better: Count cycles as sets of edges via DP over subsets, but treat each cycle as a closed path where we designate the smallest vertex as root and then the neighbor with the smaller index among the two neighbors of root is the "second vertex", and then we only count paths that go from root to that neighbor via vertices larger than root, and also enforce that the neighbor is larger than root? Hmm.
Maybe we can adopt a standard known method: Use DP for counting cycles by root as smallest and the "two-bitmask" method. I've seen a solution for counting simple cycles in undirected graphs with n up to 19 on Codeforces problem "Little Pony and Harmony Chest"? No.
Actually there is a known problem: "Counting Cycles" in UVA or atcoder? In Atcoder ABC, there is a problem "Counting Cycles" but constraints small.
Let's search memory: In Codeforces problem "Number of Simple Cycles" (maybe problem 1191F?), but not sure.
I recall a solution: For each vertex v from 0 to n-1, we consider subsets containing v and only vertices with index > v (i.e., we gradually include vertices). We compute DP[mask][u] = number of simple paths from v to u that use exactly the vertices in mask and that also have the property that the second vertex in the path (i.e., the vertex after v) is less than the last vertex? Something like that.
Alternatively, we can use DP that counts each cycle exactly once by ensuring that v is the smallest vertex and that u (the endpoint) is the smallest neighbor of v in the cycle? Let's test: In triangle with v=1, the two neighbors are 2 and 3. The smallest neighbor is 2. So we only count paths from v to 2 that use all vertices? But then the cycle would be represented by the path from 1 to 2 that goes through 3 (i.e., 1-3-2). That yields the cycle. The other path 1-2 (direct) would produce a 2-cycle not counted. So if we restrict u to be the smallest neighbor of v in the cycle (i.e., the neighbor with minimal index), then each cycle will have exactly one such representation? Let's test: In a cycle, the smallest vertex v has two neighbors; call them a and b with a < b (assuming distinct). Then we can consider the path from v to a that goes through all other vertices, i.e., the longer path that doesn't use edge (v,a) directly. That path will be the one that goes around the cycle through b, then ... eventually to a. This path includes all vertices of the cycle. However, we need to ensure we count that path exactly once. But how do we know in DP that we are taking the path that goes through the other neighbor? If we simply count all paths from v to a using the vertex set of the cycle, we get two paths: one direct edge (v,a) (if edge exists) and the other the rest of the cycle (v-b-...-a). The direct edge path yields a 2-cycle when adding edge (v,a), but that's not a valid cycle because it uses only two vertices. To avoid counting the direct edge, we need to ensure the path length is at least 2 (i.e., mask size >=3). But also we need to avoid the other direction? For a given a (the smallest neighbor), the path that goes the long way is unique? Actually there are two possible long paths from v to a: one going through b and the rest of vertices, and the other? In a simple cycle, from v to a there are exactly two simple paths: one is the direct edge (length 1), the other is the rest of the cycle (length L-1). So there is exactly one non-direct simple path of length >=2 from v to a that uses all vertices of the cycle. That's the one we want. However, if we count all paths from v to a using the vertex set of the cycle, we might also count paths that don't include all vertices? But we are summing over masks exactly equal to the cycle's vertex set. In DP we typically sum over subsets, and for a given mask equal to the vertex set of the cycle, DP[mask][a] counts the number of paths from v to a that use exactly the vertices in mask. That includes both the direct edge (if mask includes just v and a? Actually mask is the full set, so it includes all vertices. For the direct edge path, it would not use all vertices; it would use only {v, a}. So DP[mask][a] for mask = full set does NOT include the direct edge path because that path doesn't use all vertices. So it counts only paths that use all vertices in mask. That path is unique: it is the long path from v to a that visits all other vertices exactly once. So for a given cycle with smallest vertex v and smallest neighbor a, the DP count for mask=all vertices and endpoint a should be 1. For endpoint b (the larger neighbor), DP[mask][b] also equals 1 (the long path from v to b through a). So both endpoints give count 1. So if we sum over all endpoints u in mask that are neighbors of v, we get 2 per cycle. To count each cycle once, we can restrict u to be the smallest neighbor? That would give 1 per cycle. But is it always true that the smallest neighbor is well-defined and that DP[mask][smallest_neighbor] = 1? Yes. But we need to know which neighbor is the smallest among v's neighbors in the cycle. However, we are iterating over subsets and endpoints; we can incorporate a rule that when we add DP[mask][u] to answer only if u > v? Actually that doesn't work as we saw. Instead, we can require that the second vertex in the path (or the first step after v) is larger than the last vertex? Hmm.
Many implementations I've seen: For counting cycles in undirected graphs, they use the following: For each vertex i, they compute DP for subsets containing i and vertices with index > i. Then for each mask that includes i and at least two other vertices, for each j in mask, j>i, if there is an edge between i and j, they add DP[mask][j] to answer. This indeed counts each cycle exactly twice? Let's check with triangle: i=1, mask={1,2,3}, j=2 and j=3 both >1, edges (1,2) and (1,3) exist. DP[mask][2] = number of paths from 1 to 2 using all vertices in mask. That is 1 (1-3-2). DP[mask][3] = 1 (1-2-3). So answer increment 2. For squares (4-cycle): Example K4: consider cycle 1-2-4-3-1 (vertices 1,2,4,3). Smallest vertex is 1. Neighbors in cycle: 2 and 3? Actually cycle: 1-2-4-3-1, neighbors of 1 are 2 and 3. Both >1. DP[mask][2] counts paths from 1 to 2 using all vertices {1,2,3,4}: those paths must start at 1, end at 2, and visit all vertices exactly once. How many Hamiltonian paths from 1 to 2 covering all 4 vertices? In K4, number of Hamiltonian paths from 1 to 2 covering all 4 vertices: total permutations starting with 1 and ending with 2, with vertices 3 and 4 in between: 2! = 2? Actually sequences: 1-3-4-2, 1-4-3-2. So DP[mask][2] = 2. Similarly DP[mask][3] = number of Hamiltonian paths from 1 to 3 covering all vertices: 1-2-4-3, 1-4-2-3 -> 2. So each contributes 2, total 4. But we expect only 1 cycle? Wait, each 4-cycle is counted multiple times? Let's analyze: In K4, there are 3 distinct 4-cycles (undirected). For a given cycle, e.g., 1-2-4-3-1 (that's a cycle). The DP with mask = vertices of cycle (1,2,3,4) will count many Hamiltonian paths from 1 to each neighbor that cover all vertices. But note: In K4, the cycle's vertex set is all four vertices. However, there are multiple Hamiltonian paths from 1 to 2 that cover all vertices; some correspond to the cycle's order, some to other orders that might not correspond to the cycle? Let's examine: For cycle 1-2-4-3-1, the two possible paths from 1 to 2 that use all vertices and are along the cycle? Actually along the cycle, the two paths from 1 to 2 are: direct edge (1-2) which is not covering all vertices, and the other path 1-3-4-2 which uses all vertices. So that's one path (1-3-4-2). However, DP[mask][2] also counts 1-4-3-2, which corresponds to the other cycle? Let's see which cycle does 1-4-3-2 plus edge (2,1) produce? That gives cycle 1-4-3-2-1, which is another 4-cycle: 1-4-3-2-1 (or 1-2-3-4-1 depending on ordering). In K4, the cycles are: (1-2-3-4-1)?? Actually the three distinct 4-cycles in K4 are usually: 1-2-3-4-1, 1-2-4-3-1, 1-3-2-4-1. Let's list them properly: In complete graph K4, any 4-cycle is a Hamiltonian cycle of the 4 vertices. There are (4-1)!/2 = 3 undirected Hamiltonian cycles:
(1,2,3,4) -> edges: 1-2,2-3,3-4,4-1
(1,2,4,3) -> edges: 1-2,2-4,4-3,3-1
(1,3,2,4) -> edges: 1-3,3-2,2-4,4-1
So indeed three cycles. Now consider path 1-3-4-2 + edge (2,1) gives edges 1-3,3-4,4-2,2-1 which is cycle (1,3,4,2) = (1-3-4-2-1) which is same as (1-2-4-3-1) reversed? Let's see: (1-2-4-3-1) reversed is (1-3-4-2-1) which is the same cycle. So 1-3-4-2 yields the cycle (1-3-4-2-1) which is actually the reverse of (1-2-4-3-1). But (1-2-4-3-1) and (1-3-4-2-1) are the same cycle (just reversed). So both 1-3-4-2 and 1-4-3-2? Wait 1-4-3-2 + edge (2,1) gives (1-4-3-2-1) which is the cycle (1,4,3,2) reversed of (1-2-3-4-1)? Actually (1-2-3-4-1) reversed is (1-4-3-2-1). So 1-4-3-2 corresponds to cycle (1-4-3-2-1) = (1,2,3,4) reversed? Let's verify: (1,2,3,4) cycle: edges 1-2,2-3,3-4,4-1. Reverse: 1-4-3-2-1 which indeed is 1-4-3-2-1. So 1-4-3-2 yields cycle (1,2,3,4) reversed. So 1-4-3-2 is a Hamiltonian path that corresponds to a different cycle than 1-3-4-2? Actually both are Hamiltonian paths from 1 to 2. There are two such paths: 1-3-4-2 and 1-4-3-2. They correspond to two different cycles? As we see, one gives cycle (1-3-4-2-1) which is same as (1-2-4-3-1); the other gives cycle (1-4-3-2-1) which is same as (1-2-3-4-1). So these two paths correspond to two distinct cycles. So DP[mask][2] counts both cycles that include vertices 1,2,3,4 and have 1 and 2 as adjacent in the cycle? Actually both cycles have edge (1,2) as part of the cycle? Check: Cycle (1-2-4-3-1) includes edge 1-2. Cycle (1-2-3-4-1) includes edge 1-2. So both cycles contain edge (1,2). So there are two cycles that include edge (1,2) as one of the edges, among the three cycles. Indeed in K4, edge (1,2) is present in two cycles? Let's enumerate: Cycles: (1-2-3-4-1) includes (1,2); (1-2-4-3-1) includes (1,2); (1-3-2-4-1) does not include (1,2) (it has (1,3),(3,2),(2,4),(4,1)). So yes, edge (1,2) appears in two of the three cycles. So DP[mask][2] counts both cycles that include edge (1,2) and have 1 as smallest and 2 as neighbor. And DP[mask][3] counts cycles that include edge (1,3). Edge (1,3) appears in two cycles? Actually (1-2-3-4-1) includes (1,4)? Wait (1,3) appears in cycles: (1-3-2-4-1) includes (1,3); (1-3-4-2-1) includes (1,3) as well? Actually (1-3-4-2-1) is same as (1-2-4-3-1) reversed, and that cycle includes edge (1,3) ? (1-2-4-3-1) edges: 1-2,2-4,4-3,3-1 -> includes 1-3. So yes, (1,3) appears in two cycles. So DP[mask][3] counts two cycles. So total sum over j of DP[mask][j] for mask=full set is 4? Actually we found DP[mask][2]=2, DP[mask][3]=2, total 4. But there are only 3 cycles in K4? Wait we earlier said K4 has 3 cycles of length 4. But our sum for mask containing all four vertices gives 4 contributions. However, cycles of length 4 are exactly the Hamiltonian cycles on all 4 vertices. In our DP, we are counting each cycle multiple times. Let's compute: Each cycle of length 4 has smallest vertex 1 (since 1 is smallest). In that cycle, 1 has two neighbors, say a and b. For each such neighbor, there is exactly one Hamiltonian path from 1 to that neighbor covering all vertices (the path that goes the long way around). So each cycle contributes 2 to the sum (one for each neighbor). So total sum over all cycles is 2 * number_of_4_cycles = 23=6. So each cycle counted twice. Good.
Thus the naive DP that sums over all neighbors counts each cycle twice (once for each of the two neighbors of the root on the cycle). So if we divide the total sum by 2, we get the correct count? But careful: For cycles of length > 3, the root (smallest vertex) has exactly two neighbors in the cycle, so each cycle contributes exactly 2. For triangles, the root also has two neighbors, so each triangle also contributes 2. So indeed each cycle contributes exactly 2 if we count each endpoint neighbor of the root that is in the mask. So summing DP[mask][neighbor] over all neighbors (with mask containing root and at least 2 other vertices) and then dividing by 2 would give the correct number of cycles, provided we also ensure that we don't overcount cycles where the root has degree 2? Actually every simple cycle containing the root will have exactly 2 edges incident to the root in the cycle. So number of neighbors in the cycle is exactly 2. So dividing by 2 works.
But caution: What about cycles where the root has both neighbors being the same? Not possible in simple cycle.
Thus we can count each cycle exactly twice, then divide total by 2.
However, we must be careful: The DP counts paths from root to neighbor that use exactly the vertices in mask. For a cycle, the mask will be exactly the set of vertices of the cycle. For each neighbor, there is exactly one such path (the complement of the edge (root, neighbor) along the cycle). So indeed each cycle yields two contributions. So dividing total sum by 2 gives correct count.
But need to verify for cycles where root is smallest but there might be multiple paths from root to neighbor using exactly the vertex set? Could there be more than one? In a simple cycle, for a given neighbor, there is exactly one simple path from root to neighbor that visits all vertices of the cycle without using the edge (root, neighbor) directly. However, if the graph is not complete, but still a cycle, the subgraph induced by the cycle may have extra chords? Actually if the graph contains chords, then a set of vertices may contain multiple cycles. But the DP we are doing is for each subset mask, we count number of simple paths from root to w using exactly the vertices in mask. That includes paths that may use chords. For a given mask that is a superset of a cycle, there could be multiple paths from root to w that cover all vertices but not necessarily follow the cycle edges. However, we are summing over all masks. The DP will count all simple paths that start at root, end at w, and use exactly the vertices in mask. Some of these paths may not correspond to a cycle that is exactly the set of edges used plus (root,w). When we add the edge (root,w), we get a closed walk that uses vertices mask and includes the edges of the path plus edge (root,w). This closed walk may not be a simple cycle if the path plus edge creates a cycle that has chords? Actually if the graph has chords, the resulting closed walk could still be a simple cycle if the path plus edge forms a cycle with no repeated vertices (which it will, because path is simple and edge (root,w) connects the two ends, giving a cycle provided that the edge (root,w) is not already used in the path. Since the path is simple and does not include edge (root,w) because it would require w to be adjacent to root and possibly appear earlier? But the path could include the edge (root,w) as an internal edge? Since the path ends at w, and starts at root, if the path contains the edge (root,w) somewhere in the middle, that would imply the path visits root, then later visits w, then continues, but since root is start, if the path includes edge (root,w) as an internal edge, that would mean from root we go to w and later come back to root? That would repeat vertices. So simple path cannot contain edge (root,w) unless it's the last edge? Actually a simple path from root to w could have the edge (root,w) only if it is the first edge (i.e., path length 1). But we require mask size >=3, so path length at least 2, so edge (root,w) is not used. So path plus edge (root,w) gives a cycle that uses all vertices in mask. However, the set of edges of this cycle is exactly the edges of the path plus (root,w). This might not be a simple cycle if the path already includes some chords that cause the cycle to have additional chords? Actually a simple cycle is defined as a closed walk with no repeated vertices or edges. The constructed closed walk is a cycle (simple) because it's a loop that goes from root to w along the path, then back to root via the edge (root,w). It uses distinct vertices (the path vertices are distinct and root is only at start, w at end; adding edge back to root doesn't introduce new vertices). So it's a simple cycle. However, the cycle may contain chords? If the graph has extra edges among vertices in mask, the cycle might not be induced; but that's okay, it's still a simple cycle as long as edges used are path edges and (root,w). The fact that there are extra edges in the graph not used does not disqualify it. So each such path + edge yields a simple cycle. But could the same cycle be generated from different (mask, w) pairs? Possibly if there are multiple ways to partition the cycle into a path from root to some neighbor w that uses all vertices. For a given cycle with smallest vertex root, there are exactly two such partitions: one for each neighbor. So each cycle yields exactly two (mask,w) pairs, where mask = vertex set of cycle, w = each of the two neighbors. So dividing by 2 works.
But what about cycles where the smallest vertex has degree >2 in the graph, but the cycle uses only two of its incident edges. Still two neighbors.
Thus algorithm: For each vertex v from 0 to n-1 (or 1 to n), treat v as smallest vertex. We will consider subsets mask that include v and only vertices with index > v (to avoid counting cycles multiple times with different smallest vertices). Actually we restrict the DP to vertices with index > v, because if we include vertices smaller than v, then the smallest vertex in that cycle would not be v, leading to double counting when we later consider that smaller vertex as root. So we ensure mask only contains v and vertices with higher indices. Then for each such mask, for each w in mask with w > v and edge(v,w) exists, we add DP[mask][w] to total cycles. Finally answer = total cycles / 2.
We need to compute DP[mask][w] for masks containing v and w and possibly other vertices, where v is the root and all vertices in mask are >= v? Actually we restrict to vertices with index >= v? Since we include v and others > v. That's typical.
Now DP initialization: DP[1<<v][v] = 1. Then we iterate over masks that include v and have size >=2, and for each mask, for each u in mask (where u is the last vertex of the path), we consider extending the path by adding a neighbor x that is not in mask and x > v? Actually we need to ensure that we only consider vertices x that are > v? Because if we allow vertices smaller than v, then the mask could include a vertex smaller than v, which would make v not the smallest in that mask, causing cycles to be counted when v is not the smallest. However, when we later process root = smaller vertex, that cycle would also be counted. To avoid double counting, we restrict to vertices with index > v. So in DP for root v, we only consider vertices with index > v, and also the root v is the only vertex with index possibly <= v (since v is smallest). So we only add vertices with index > v to mask. This ensures that any cycle counted with root v has v as the smallest vertex (since all other vertices are larger). That's crucial.
Implementation details: Let's label vertices 0-indexed from 0 to n-1 (or 1 to n, but easier 0-index). For each root = 0..n-1, we will consider DP over subsets of vertices from root+1..n-1 plus root. So the total number of vertices we consider for root is at most n - root. Since n≤19, total over all roots is sum_{v=0}^{n-1} 2^{n-1-v} = O(2^n) actually sum_{v} 2^{n-v} ≈ 2^{n+1}. That's about 2^20 = 1M, times n maybe. So it's manageable.
We need to compute DP[mask][u] for each mask that includes root and maybe other vertices > root. Mask is a bitmask of size up to n (19 bits). Could store DP as an array of size 2^n for each u? But we are iterating root separately, so we can reuse a DP array. Since for each root, the set of allowed vertices is different (only vertices > root), we can treat the DP as over masks of the reduced set. But we can still use global bitmask of all n bits, but restrict that bits for vertices < root are never set except root's bit. So we can still use full mask of n bits, but we ensure that we never set bits for vertices < root other than root. That might be okay.
Simplify: For each root, we compute DP over masks where the only possible set bits are root and vertices with index > root. So we can create a mapping from these allowed vertices to bits in a smaller mask to save memory/time. But with n=19, full mask 2^19=524288, and DP for each root would be an array of that size times n, which times 19 would be about 19*524288 ≈ 10 million, which is okay. But we also need to iterate over masks and neighbors. However, if we do for each root separately, total time would be sum over roots of (2^{(n - root)} * (n - root) * degree) etc. That could be up to ~ 19 * 2^18 = 19 * 262144 ≈ 5 million? Actually sum_{v=0}^{n-1} 2^{n-v} = 2^{n+1} - 2 ≈ 2^20 ≈ 1e6. So total DP states across all roots is about 1e6. For each state we iterate over possible extensions, each neighbor. That could be O(2^n * n * n) maybe, still fine.
But we need to compute DP[mask][u] for each mask that includes root, and for each u in mask, the number of simple paths from root to u using exactly the vertices in mask. We'll compute via DP over increasing mask size. Standard DP: DP[mask][u] = sum_{v in mask, v != u, edge(v,u)} DP[mask ^ (1<<u)][v]. We'll need to ensure that root is the smallest index in mask? Actually we only consider masks where the smallest set bit is root (since we only include vertices > root). But we can enforce in transitions that we only add vertices > root. And we only start with mask = (1<<root). Then we iterate over masks that include root. For each mask, for each u in mask (u != root maybe), if DP[mask][u] > 0, we can try to extend to a neighbor x not in mask, with x > root. But we need to compute DP for all masks. We can compute DP by iterating over masks in increasing order of size, and for each u where DP[mask][u] > 0, we add transitions to neighbors x not in mask (and x > root). However, we also need to later use DP[mask][w] to count cycles, for masks of size >=3 (since cycle length at least 3). So we can compute DP and simultaneously accumulate cycles.
Pseudo:
ans = 0
For root in 0..n-1:
DP = array[1<<n][n] initialized to 0 (or we can use dictionary for smaller masks)
DP[1<<root][root] = 1
For mask from 1 to (1<<n)-1:
if mask includes root and (mask's smallest index? Actually we only want masks that have root and only vertices > root, but we can skip masks that include vertices < root other than root. So we can check: if (mask & ((1<<root)-1)) != 0: continue. Because bits for vertices lower than root should be 0 except root's bit? Actually root's bit is at position root. The lower bits are indices < root. So we require that mask has no bits set for indices < root. So condition: mask & ((1<<root)-1) == 0.
Also ensure that root bit is set (mask & (1<<root)).
For each u in [0..n-1] such that mask has u and DP[mask][u] > 0:
// Option 1: if there is an edge between root and u, and mask size >= 3, we can add DP[mask][u] to cycles (tentative)
if edge[root][u] and mask_size >= 3:
ans += DP[mask][u]
// Option 2: extend to neighbor x not in mask, with x > root
for each neighbor x of u:
if x < root: continue // skip vertices smaller than root
if (mask >> x) & 1: continue // already visited
new_mask = mask | (1<<x)
DP[new_mask][x] += DP[mask][u]
// End for root
After processing all roots, total_cycles = ans // 2
We must be careful: In the cycle counting step, we add DP[mask][u] when edge[root][u] exists and mask size >=3. But is it correct to add for all u? According to our reasoning, we add for each u that is neighbor of root, and u is the endpoint of a path from root that uses vertices mask, and we are about to close the cycle with edge (root,u). This cycle will have smallest vertex = root (since mask only contains vertices >= root, and root is smallest). But we need to ensure that we don't count cycles where the path includes the edge (root,u) somewhere else? As argued, it can't because mask size >=3 ensures path length at least 2, so the direct edge (root,u) is not part of the path. Also, we need to make sure that we count each cycle exactly twice (once per neighbor). Our addition adds DP[mask][u] for each neighbor u, which includes both neighbors of root in the cycle. So that's fine.
However, there is a subtlety: For a given mask (set of vertices) and u, DP[mask][u] counts number of simple paths from root to u using exactly vertices in mask. Could there be multiple paths from root to u using the same vertex set mask? Yes, if there are multiple ways to traverse the vertices (due to chords). That's fine: each such path, when closed with edge (root,u), yields a distinct simple cycle? Possibly different cycles could be formed from the same vertex set and same endpoints but using different edges. Are they considered distinct cycles? Yes, because the cycle is defined by its set of edges, and different paths correspond to different sets of edges (since the edges used differ). So each such path corresponds to a distinct cycle (since path edges differ). So we should count each such path as a separate cycle (as long as the resulting cycle is simple). So DP[mask][u] counts the number of distinct cycles that have smallest vertex root, vertex set exactly mask, and include edge (root,u) as the closing edge, with the rest of edges given by the path. However, could the same cycle be generated from two different (mask, u) pairs? As we argued, each cycle has exactly two such representations: one for each neighbor of root on the cycle. Also, the same cycle might be generated with the same mask but different u? Only the two neighbors. So dividing by 2 works.
But we must also verify that a cycle might be counted with a mask that is a superset of its vertex set? For example, suppose we have a cycle on vertices {v,a,b} (triangle) but the graph also has extra vertex c (connected somehow). If we have a path from v to a that goes through b and also includes c (so mask includes c as well), then adding edge (v,a) yields a closed walk that visits extra vertex c, which is not a simple cycle because it has more vertices than the cycle? Actually the resulting closed walk would be v -> ... -> a -> v, and it includes all vertices in mask. Since mask includes extra vertices beyond the cycle, the walk goes through all those vertices. That walk is a simple cycle only if the extra vertices are visited exactly once and the walk returns to start, but does that form a cycle? A cycle is defined as a closed walk with no repeated vertices. That's exactly a Hamiltonian cycle on the vertex set mask. But if mask includes extra vertices, the resulting closed walk is a cycle that includes those extra vertices. That's a different cycle (a longer cycle). So it's fine; it's not duplicate of the triangle.
Thus DP approach works.
Now, complexity: For each root, we iterate over all masks that include root and have no bits < root. Number of such masks is 2^{(n-1-root)} * (the presence of root mandatory). Actually with root fixed, the remaining vertices are indices root+1..n-1, count = n-1-root. So number of masks = 2^{k} where k = n-1-root. Sum over roots gives total masks ≈ 2^{n}. Actually sum_{i=0}^{n-1} 2^{i} = 2^n - 1 ≈ 524k for n=19 (since 2^19=524288). So total DP states across roots is about 1 million. For each mask, we iterate over u where DP[mask][u] nonzero. In worst case, many u may have nonzero DP. But we can iterate over all u in mask, but we can also iterate over u that have DP>0 by checking. Since n≤19, we can just loop over all u (0..n-1) for each mask. So per mask, O(n) for enumerating u, and for each u we extend to neighbors (degree). So total time per root is O(3^{k}?) Actually more like O(2^{k} * n * avg_deg). With n=19, k up to 18, that's acceptable.
But we must be careful about memory: DP[mask][u] for each root we can recompute and not store all masks simultaneously? Actually for each root, we need DP for all masks. We can allocate an array of size (1<<n) * n, which is about 524288 * 19 ≈ 10 million integers. That's about 40 MB if using 4-byte ints. For each root we would reallocate? That would be 19 * 40 MB = 760 MB if we keep all at once, but we can process each root separately, allocating a fresh DP each time, but that would still be 40 MB per iteration, which is fine. However, we could also use only masks that include root and vertices > root. But it's easier to allocate a full DP array of size (1<<n) * n for each root and initialize to zero. Since we have up to 19 roots, we might run time to clear the array each time. But we can instead allocate a single DP array and reuse, but we need to reset entries for relevant masks. Since total number of masks across roots is about 2^n, we can just recompute for each root using a fresh vector sized to 1<<n? That would be 524288 entries each with n values? Actually we can store DP[mask] as an array of n values; but that's 524k * 19 ≈ 10M entries. That's okay. We'll allocate as vector<vector> or 2D array. But we need to be careful about memory: 10M * 8 bytes = 80 MB if using long long (since answer may be large? The number of cycles could be huge: maximum number of simple cycles in a complete graph of 19 vertices. That's enormous. Actually number of simple cycles in K_n is sum_{k=3}^{n} \frac{n!}{2k (n-k)!}. For n=19, it's astronomically large (maybe > 2^63?). But constraints? n≤19, so maximum cycles might be extremely large. Let's estimate: number of Hamiltonian cycles alone is (n-1)!/2 ≈ 18!/2 ≈ 6.4e15, which fits within 64-bit? 18! is about 6.4e15, half is 3.2e15, fits in 64-bit (max 9e18). But total cycles includes cycles of all lengths; sum_{k=3}^{n} C(n,k)*(k-1)!/2. For n=19, the largest term is k=n: (19-1)!/2 = 18!/2 ≈ 3.2e15. Next term: k=18: C(19,18)*17!/2 = 19 * (17!/2) ≈ 19 * 1.27e14 = 2.4e15. Summed up maybe around 1e16? Actually 19 * 17!/2 ≈ 19 * 1.27e14 = 2.41e15. Then k=17: C(19,17)*16!/2 = 171 * (16!/2) = 171 * 1.02e13 = 1.74e15. So total maybe around 1e16, still fits in 64-bit (max ~9.22e18). But is there possibility of more cycles due to chords? Actually in complete graph, number of simple cycles is sum_{k=3}^{n} \frac{n!}{2k (n-k)!}. For n=19, we can compute roughly: n! = 1.216e17, dividing by 2k ~ up to 38 gives ~3.2e15. Summing over k gives maybe around 1e16. So 64-bit unsigned should suffice. Use unsigned long long.
But we must be careful about overflow? Possibly still within 2^64-1 (1.8e19). For n=19, it's safe. For n=20 it would be bigger? But n≤19, so fine.
Thus we can store DP as 64-bit ints.
But we also need to be careful about double counting cycles that might be counted with different roots? Our root selection ensures each cycle has a unique smallest vertex, so cycles counted for different roots are disjoint. However, we also count cycles within each root with the DP that includes only vertices > root. So no overlapping across roots. Then we sum contributions across roots, and then divide by 2? Wait, we need to divide by 2 only within each root? Actually each cycle will be counted twice when processing its smallest vertex root: once for each neighbor of root in the cycle. So if we sum over all roots (with the DP restricted to vertices > root), each cycle is counted exactly twice overall (since its smallest vertex is root). So after summing contributions from all roots, we divide total by 2. That yields correct total.
But we must also ensure that cycles of length 3 (triangles) are also counted twice? Yes.
Let's test on K4 with root 0 (vertex 1). Our DP for root 0 will consider masks containing 0 and vertices >0 (i.e., 1,2,3). For mask of size 3 (triangle) like {0,1,2}: DP[mask][1] and DP[mask][2] etc. Let's compute: For triangle {0,1,2}, there are paths from 0 to 1 using vertices {0,1,2}: possible paths: 0-2-1 (if edges exist). In K4, all edges exist, so DP[{0,1,2}][1] = 1 (0-2-1). DP[{0,1,2}][2] = 1 (0-1-2). Edges (0,1) and (0,2) exist, so contributions: from mask {0,1,2}, add DP[mask][1] and DP[mask][2] because mask size 3. So adds 2. Similarly for triangles {0,1,3} and {0,2,3}. So for root 0, contributions from triangles: 3 triangles * 2 = 6. For 4-cycles: mask = {0,1,2,3}, contributions from neighbors 1,2,3: DP[mask][1] = number of Hamiltonian paths from 0 to 1 covering all vertices. In K4, there are 2 (0-2-3-1, 0-3-2-1). Similarly DP[mask][2]=2, DP[mask][3]=2, sum =6. So total contributions for root 0 = 6+6=12. For root 1, we consider vertices >1 (i.e., 2,3). Triangles containing 1 as smallest? Triangles that include 1 as smallest vertex are those where the other two vertices are both >1, i.e., triangle {1,2,3}. So root=1 will count triangle {1,2,3} (once as neighbors 2 and 3? Actually triangle {1,2,3} with vertices 1,2,3: root=1, mask {1,2,3} contributes DP[mask][2] and DP[mask][3] each 1? Let's compute: DP[mask][2] = paths from 1 to 2 covering all vertices: 1-3-2 = 1. DP[mask][3] = 1-2-3 =1. So adds 2. So root 1 contributes 2. Root 2: vertices >2 only 3. No triangle with vertices {2,3,?} need 3 vertices, but only {2,3,?} with smallest 2 would require another vertex >2, but there is only 3, so no. So root 2 contributes 0. Root 3: no vertices >3, contributes 0. Total contributions across all roots: 12+2=14. Dividing by 2 gives 7. That's correct.
Thus algorithm works.
Now we need to implement DP efficiently.
Implementation plan:
- Read n, m.
- Represent graph as adjacency list, also adjacency matrix for quick edge check.
- We'll index vertices from 0 to n-1.
- total_cycles = 0 (unsigned long long).
- For root from 0 to n-1:
- Create DP array: dp[mask][v] maybe using vector<vector>? But mask size up to 1<<n = 524288, n=19 => 524288 * 19 ~ 10M entries. Using 64-bit, that's 80 MB. That's high but maybe acceptable? Actually 10M * 8 = 80 MB. For each root we would allocate new 80 MB, but we can reuse a single DP array across roots by resetting only used masks. But we can also compress: Since for each root we only consider masks where bits for vertices < root are 0 (except root). So the number of masks we actually process is 2^{n-1-root} (including root bit). That's at most 2^{18}=262144 for root=0, but sum across roots is ~ 2^n = 524288. So we could allocate DP for all masks but only iterate over those masks. However, memory for full DP is 80 MB, which is okay for many programming contest environments (like Codeforces 256 MB). But we need to be careful about time clearing DP for each root. If we allocate a new 2D vector for each root, that would be 19*80 MB = 1.5 GB allocation total (though not simultaneously, we could allocate and deallocate each iteration). But allocation of 80 MB per iteration may be heavy but possibly fine in C++ if using vector and clearing. Alternatively, we can allocate a single dp array of size (1<<n) * n and reuse. At start of each root, we need to clear the entries for masks that we will use. Since we only access masks that have root bit and no smaller bits, we can set dp[mask][v] = 0 when we first write. But we might need to zero out all entries for those masks. However, number of masks we process is about 2^{n} total across all roots, so we can just assign 0 when we process mask, but we need to ensure we don't read uninitialized values. A simpler approach: Use a 2D array dp[1<<n][n] as static and set to 0 before each root using memset for the relevant portion? For root=0, we need all masks that have bit0 set? Actually for root=0, allowed masks are those with bit0 set and no restriction on lower bits (since none lower). So we need to clear all masks that have bit0 set? That's half of all masks. For root=1, we need masks where bit1 set and bits<1 (i.e., bit0) are 0. That's a smaller set. Instead of clearing whole dp each time (which would be 80 MB * 19 = 1.5 GB of writes), we could use a timestamp method: have dp[mask][v] store a value and a version flag to indicate if it's for current root. But that's messy.
Given n≤19, 2^n = 524288, n=19, dp array of size 524288 * 19 = 9,961,472 entries. Using unsigned long long (8 bytes) = about 80 MB. This is acceptable if we allocate once and reuse. Then for each root, we need to clear only the entries for masks that we will potentially read/write. Since we iterate over masks in increasing order, we can reset dp[mask][v] to 0 at the start of each mask iteration for all v? But that would be heavy. Better to iterate over masks that we will actually use and set dp[mask][v] to 0 when we assign new values; but we need to ensure that values from previous root don't interfere. We can clear the entire dp array before each root using memset, which would be 80 MB per root -> 19 * 80 MB = 1.52 GB of memory writes. In C++, memset of 80 MB is fast? 80 MB ~ 0.08 GB, 19 times is 1.5 GB, which might be okay in 1 second? Possibly borderline but likely fine given time limits maybe 2 sec? But we can optimize.
We can reduce memory by noting that for each mask, we only need dp for vertices that are in the mask? Not necessarily; we store for each possible endpoint v (0..n-1) but many entries are zero because v not in mask. However, we can store dp as a vector of length n for each mask, but that's what we have. Alternatively, we can store dp as an array of unsigned long long dp[1<<n] but for each mask we store an array of n? Actually we can store as dp[mask] being a vector of length n allocated dynamically only for masks we use, but that could be more overhead.
Given n=19, I think full dp of 80 MB is okay. Clearing with memset for each root (80 MB * 19 = 1.5 GB) might be okay if optimized. But we can also avoid clearing the entire dp by using a separate array "vis" to indicate if dp entry belongs to current root. But that's more complex.
Another idea: For each root, we can compute DP using iterative DP over masks and only store dp for current mask? Actually we need dp[mask][v] for many masks simultaneously because we use dp[mask] to transition to larger masks. We can do DP in increasing mask size and store dp[mask] as array of n. We need to access dp[mask] for smaller masks. So we need to keep all dp values for all masks computed so far? Actually we can compute dp[mask] from subsets mask without w, so we need dp[mask ^ (1<<w)] values. So we need dp for all smaller masks. So we need to store all dp values.
Thus we need the full table.
But we can reduce memory by using dp[mask] as a single integer per mask representing number of paths? No, we need per endpoint.
Maybe we can store dp[mask] as an array of n 64-bit ints, but we can compress into a single 64-bit if we store counts for all endpoints in a bitset? Not possible.
Given constraints, 80 MB is okay if we allocate once and then for each root we clear only the masks we use. Since we know exactly which masks are relevant for a given root: those masks that have root bit set and no bits < root. For root, the number of such masks is 2^{n-1-root}. For each such mask, we need to clear dp[mask][v] for all v. If we iterate over those masks and set to zero for all v, that would be sum over roots of (2^{k} * n) where k = n-1-root. That's about sum_{k=0}^{n-1} 2^{k} * n ≈ n * 2^n = 19 * 524288 ≈ 10M entries. That's about 10M * 8 = 80 MB of writes total (since each entry cleared once across all roots). Actually careful: For each root, we need to clear the dp entries for all masks we will use in that root. But after processing root, we don't need those values anymore; we can just overwrite them in next root without clearing if we ensure we set each dp entry before reading. But when we compute dp for a root, we will write to dp[mask][v] for masks we process. We need to ensure that dp entries not written in this root are not read. Since we only read dp[mask][v] for masks that we have previously written (smaller masks). So as long as we always compute dp in increasing mask order and only read from masks that have already been computed in the same root, it's safe if we zero out the starting mask only. But we also need to ensure that dp entries from previous root are not mistakenly read. Since we only iterate over masks that are relevant for current root, and we only read dp[mask][v] for masks that are subsets of current mask and also relevant (they also satisfy the root condition). For a given root, all masks we consider have no bits < root except root. So any subset mask' of such a mask will also have no bits < root except root (since removing bits cannot introduce lower bits). So those masks are also in the set we will process. However, dp[mask'] may still contain values from previous root if we haven't cleared them. But if we recompute dp in increasing order of mask size, we will compute dp[mask'] before using it. So we need to ensure that when we compute dp[mask'], we set it to correct values (by summing over neighbors) and not use old values. So we need to reinitialize dp for each mask' from scratch. That means we must clear dp[mask'] before computing it. But if we compute dp[mask'] in order (starting from smallest mask), we can set dp[mask'][v] = 0 initially and then compute from transitions. But we need to ensure that we don't have leftover values from previous masks. So we need to either clear each dp[mask'] before using it, or before starting root we clear all relevant masks.
Simpler: For each root, we can iterate over all masks (from 0 to (1<<n)-1) but skip those not meeting condition. For each mask we compute dp[mask] based on subsets. To avoid having to clear all masks, we can allocate dp as a vector<vector> and for each root we reassign it to zero by using .assign() or nested loops. But that would be 80 MB of writes per root anyway.
Given the total writes across all roots if we clear only used masks is similar to total number of dp entries we actually compute. But we may also need to clear dp[mask][v] for masks we don't use? Not necessary if we skip them.
I'll design as follows:
- Precompute adjacency matrix adj[n][n] as bool.
- total = 0 (unsigned long long)
- For root in 0..n-1:
- Let allowed_mask = bits for vertices >= root? Actually we only allow vertices > root and root itself.
- We'll create dp as a 2D array: vector<vector> dp(1<<n, vector(n, 0)); But that's too large per root (80 MB). But we can allocate once and then for each root we clear only the masks we use. But easier: we can use a map from mask to vector? That would be slower.
Better: Use dp as an array of type unsigned long long* dp = new unsigned long long(1<<n)*n; (initialized to zero). Then for each root we can process.
But if we allocate fresh each root, memory allocation 19 times 80 MB might be okay if we deallocate each time? But allocation of 80 MB 19 times might be heavy but still possible (1.5 GB allocation). Usually it's okay if we use vector and let it go out of scope each iteration.
But I think it's fine to allocate a 2D array of size (1<<n) x n as static and use memset for the portion we need. But we can also use vector and assign zero for each mask we encounter. Another approach: Since n is small, we can use DP on the fly using recursion with memoization, but that would be slower.
Given n=19, we can actually compute DP for each root using bitmask iteration over subsets of the set of vertices greater than root. We can map those vertices to indices 0..k-1, and use DP on a smaller mask of size k. This reduces memory drastically. For each root, we have k = n - 1 - root. So maximum k is 18 when root=0. The number of masks for that root is 2^k (about 262k). For each mask, we need to store DP values for each possible endpoint, which is among the k+1 vertices? Actually endpoints can be any vertex in the mask, including root and the others. So we need dp[mask][endpoint], where endpoint is one of the vertices in the mask. Since vertices are original indices, we could map them to local indices. We can create an array "ids" that lists the vertices we consider: root plus all vertices > root. Suppose we have list = [root, v1, v2, ..., vk] where v_i > root. Then we can map each vertex to its position in this list (0..k). Then DP can be done on mask of size (k+1) bits? Actually total vertices considered = k+1 (including root). But we can treat mask over those local indices, with bit 0 representing root, bits 1..k representing the other vertices. Then we need dp for masks up to 2^{k+1} which is 2^{n-root} (since root + (n-1-root) = n). Actually k = n-1-root, so total considered = k+1 = n-root. So number of masks = 2^{n-root}. For root=0, that's 2^{19} = 524288, same as before. So no reduction: the total number of masks across all roots is sum_{r=0}^{n