RainX_69

Count Lucky Permutations (IMPORTANT GRAPH PROBLEM)

Jan 14th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | Source Code | 0 0
  1. LINK -> https://practice.geeksforgeeks.org/problems/e9e2da3de3eb35679ca7e17b752ae877635f1a26/1?utm_source=youtube&utm_medium=courseteam_practice_desc&utm_campaign=problem_of_the_day
  2.  
  3. --------------------------------------------------------------------------------------------------------------------------------------
  4.  
  5. You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list.
  6. Your task is to find the number of lucky permutations of the given array.
  7.  
  8. An array permutation is said to be lucky if for every node Vi  [1 <= i <= N-1] in the array there exists an edge between the nodes Vi and Vi+1 in the given graph.
  9.  
  10. Example 1:
  11. Input:
  12. N = 3, M = 2
  13. arr = {1, 2, 3}
  14. graph = {{3, 1}, {1, 2}}
  15. Output:
  16. 2
  17. Explanation:
  18. All possible permutations of the
  19. array are as follows-
  20. {1,2,3}: There is an edge between 1 and
  21. 2 in the graph but not betwen 2 and 3.
  22.  
  23. {2,1,3}: There is an edge between (2,1)
  24. and (1,3) in the graph.
  25.  
  26. {3,1,2}: There is an edge between (3,1)
  27. and (1,2) in the graph.
  28.  
  29. Out of the 3 possible permutations,
  30. 2 are lucky. Therefore, answer is 2.
  31.  
  32.  
  33. Example 2:
  34. Input:
  35. N = 2, M = 1
  36. arr = {1, 1}
  37. graph = {{1, 2}}
  38. Output :
  39. 0
  40. Explanation:
  41. There is no lucky permutation in the
  42. given graph.
  43.  
  44.  
  45. Constraints:
  46. 2 ≤ N ≤ 15
  47. 1 ≤ M ≤ (N*(N-1))/2
  48. 1 ≤  arr[i], graph[i][j] ≤ N
  49. There are no self-loops and repeated edges in the graph.
  50.  
  51.  
  52.  
  53. ---------------------------------------------------------------------------------------------------------------------------------------
  54.  
  55.  
  56. class Solution {
  57.   private:
  58.     vector<vector<bool>> conn;
  59.     long long int dp[17][1<<15];
  60.   public:
  61.     long long int helper(vector<int> &arr, int mask, int prev, int N){
  62.         if(mask==(1 << N)-1){
  63.             return 1;
  64.         }
  65.         if(dp[prev][mask]!=-1){
  66.             return dp[prev][mask];
  67.         }
  68.         long long int res=0;
  69.         for(int i=0;i<N;i++){
  70.             if(mask & (1 << i)){  // if ith bit is visited already, then do not take it
  71.                 continue;
  72.             }
  73.             if(prev==16 || conn[arr[prev]][arr[i]]==true){ // if prev OR there is an edge between prev element and current element
  74.                 res+=helper(arr,mask | (1 << i),i,N);
  75.             }
  76.         }
  77.         return dp[prev][mask]=res;
  78.     }
  79.    
  80.     long long int luckyPermutations(int N, int M, vector<int> arr, vector<vector<int>> graph) {
  81.         conn.resize(N+1,vector<bool>(N+1,false));
  82.         memset(dp,-1,sizeof(dp));
  83.         for(auto g: graph){
  84.             conn[g[0]][g[1]]=true;
  85.             conn[g[1]][g[0]]=true;
  86.         }
  87.         return helper(arr,0,16,N);  
  88.         /* You can use prev=-1 but you need to check if prev!=-1
  89.            everytime before accessing from dp. better use 16 instead */
  90.     }
  91. };
  92.  
  93.  
  94. uses Bitmask to keep track of indexes since indexes are in range 0 to 15 at max, so we can use bitmasking to keep track of index, keep prev index to see what element you are having prev and if it has connection with current one
Advertisement
Add Comment
Please, Sign In to add comment