-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count_Lucky_Permutation.cpp
34 lines (31 loc) · 1.13 KB
/
Count_Lucky_Permutation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public:
long long int luckyPermutations(int N, int M, vector<int> arr,vector<vector<int>> graph) {
vector<vector<long long int>> dp(N, vector<long long int>(1<<N, 0));
for (int i = 0; i < N; i++) {
dp[i][1 << i] = 1;
}
vector<vector<int>> adj(N, vector<int>(N, 0));
for (vector<int> edge : 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))) {
for (int j = 0; j < N; j++) {
if (j!= i && arr[j]!=arr[i] &&
(1 & (bitmask >> j)) && adj[arr[i]-1][arr[j] - 1]) {
dp[i][bitmask] += dp[j][bitmask ^ (1 << i)];
}
}
}
}
}
long long int ans = 0;
for (int i = 0; i < N; i++) {
ans += dp[i][(1 << N) - 1];
}
return ans;
}
};