-
Notifications
You must be signed in to change notification settings - Fork 1
/
13_jan.java
34 lines (29 loc) · 899 Bytes
/
13_jan.java
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 {
long luckyPermutations(int N, int M, int arr[], int[][] graph) {
// Code here
long[][] dp=new long[N][1<<N];
boolean[][]adj=new boolean[N][N];
for(int i=0;i<M;i++){
adj[graph[i][0]-1][graph[i][1]-1]=true;
adj[graph[i][1]-1][graph[i][0]-1]=true;
}
for(int i=0;i<N;i++){
dp[i][1<<i]=1;
}
for(int bitmask=1;bitmask<(1<<N);bitmask++){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(i!=j && adj[arr[i]-1][arr[j]-1] && arr[i]!=arr[j]
&& (1 & (bitmask>>j))>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;
}
}