using
System;
using
System.Collections.Generic;
public
class
GFG {
static
long
CountPermutations(
int
N,
int
M,
List<
int
> arr,
List<List<
int
> > graph)
{
List<List<
long
> > dp =
new
List<List<
long
> >();
for
(
int
i = 0; i < N; i++) {
dp.Add(
new
List<
long
>());
for
(
int
j = 0; j < (1 << N); j++) {
dp[i].Add(0);
}
}
for
(
int
i = 0; i < N; i++) {
dp[i][1 << i] = 1;
}
int
[, ] adj =
new
int
[N, N];
foreach
(List<
int
> edge
in
graph)
{
adj[edge[0] - 1, edge[1] - 1] = 1;
adj[edge[1] - 1, edge[0] - 1] = 1;
}
for
(
int
bitmask = 1; bitmask < (1 << N);
bitmask++) {
for
(
int
i = 0; i < N; i++) {
if
((1 & (bitmask >> i)) != 0) {
for
(
int
j = 0; j < N; j++) {
if
(j != i && arr[j] != arr[i]
&& (1 & (bitmask >> j)) != 0
&& adj[arr[i] - 1, arr[j] - 1]
!= 0) {
dp[i][bitmask]
+= dp[j]
[bitmask ^ (1 << i)];
}
}
}
}
}
long
ans = 0;
for
(
int
i = 0; i < N; i++) {
ans += dp[i][(1 << N) - 1];
}
return
ans;
}
static
public
void
Main()
{
int
N = 3, M = 2;
List<
int
> arr =
new
List<
int
>() { 1, 2, 3 };
List<List<
int
> > graph =
new
List<List<
int
> >() {
new
List<
int
>() { 3, 1 },
new
List<
int
>() { 1, 2 }
};
long
ans = CountPermutations(N, M, arr, graph);
Console.WriteLine(ans);
}
}