<p>We have <code>n</code> buildings numbered from <code>0</code> to <code>n - 1</code>. Each building has a number of employees. It&#39;s transfer season, and some employees want to change the building they reside in.</p>

<p>You are given an array <code>requests</code> where <code>requests[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represents an employee&#39;s request to transfer from building <code>from<sub>i</sub></code> to building <code>to<sub>i</sub></code>.</p>

<p><strong>All buildings are full</strong>, so a list of requests is achievable only if for each building, the <strong>net change in employee transfers is zero</strong>. This means the number of employees <strong>leaving</strong> is <strong>equal</strong> to the number of employees <strong>moving in</strong>. For example if <code>n = 3</code> and two employees are leaving building <code>0</code>, one is leaving building <code>1</code>, and one is leaving building <code>2</code>, there should be two employees moving to building <code>0</code>, one employee moving to building <code>1</code>, and one employee moving to building <code>2</code>.</p>

<p>Return <em>the maximum number of achievable requests</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/10/move1.jpg" style="width: 600px; height: 406px;" />
<pre>
<strong>Input:</strong> n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
<strong>Output:</strong> 5
<strong>Explantion:</strong> Let&#39;s see the requests:
From building 0 we have employees x and y and both want to move to building 1.
From building 1 we have employees a and b and they want to move to buildings 2 and 0 respectively.
From building 2 we have employee z and they want to move to building 0.
From building 3 we have employee c and they want to move to building 4.
From building 4 we don&#39;t have any requests.
We can achieve the requests of users x and b by swapping their places.
We can achieve the requests of users y, a and z by swapping the places in the 3 buildings.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/10/move2.jpg" style="width: 450px; height: 327px;" />
<pre>
<strong>Input:</strong> n = 3, requests = [[0,0],[1,2],[2,1]]
<strong>Output:</strong> 3
<strong>Explantion:</strong> Let&#39;s see the requests:
From building 0 we have employee x and they want to stay in the same building 0.
From building 1 we have employee y and they want to move to building 2.
From building 2 we have employee z and they want to move to building 1.
We can achieve all the requests. </pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 20</code></li>
	<li><code>1 &lt;= requests.length &lt;= 16</code></li>
	<li><code>requests[i].length == 2</code></li>
	<li><code>0 &lt;= from<sub>i</sub>, to<sub>i</sub> &lt; n</code></li>
</ul>

class Solution {
public:
    int yes(int idx, int mask, vector<int>& dp, int n, vector<vector<int>>& requests) {
        int nn = requests.size();
        if (idx == nn) {
            int count=0;
            vector<int> balance(n, 0);
            for (int i = 0; i < nn; ++i) {
                if (mask & (1 << i)) {
                    count++;
                    balance[requests[i][0]]--;
                    balance[requests[i][1]]++;
                }
            }
            
            for (int i = 0; i < n; ++i) {
                if (balance[i] != 0) {
                    return -10000;
                }
            }
            return count;
        }
        if (dp[mask] != -1) {
            return dp[mask];
        }

        int ans = 0;
        ans = max(ans, yes(idx + 1, mask, dp, n, requests));
        int newMask = mask | (1 << idx);
        ans = max(ans, yes(idx + 1, newMask, dp, n, requests));

        return dp[mask] = ans;
    }

    int maximumRequests(int n, vector<vector<int>>& requests) {
        int nn = requests.size();
        int maskSize = (1 << nn);
        vector<int> dp(maskSize, -1);
        int ans = yes(0, 0, dp, n, requests);
        return ans;
    }
};


// class Solution {
// public:
//     int yes(int idx,int mask,vector<int>& dp,int n,vector<vector<int>>& requests)
//     {
//         int nn=requests.size();
//         if(idx==nn)
//         {
           
//             vector<int> aa(n,0);
//             for(int i=0;i<nn;i++)
//             {
//                 if((mask & (1<<i))==1)
//                 {
//                     aa[requests[i][0]]--;
//                     aa[requests[i][1]]++;
//                 }

//             }
//             for(int i=0;i<n;i++)
//             {
//                 if(aa[i]!=0)
//                 {
                     
//                     return -10000;
//                 }
//             }
           
//             return 0;
//         }
//         else if(dp[mask]!=-1){
//             return dp[mask]; 
//         }
//         else
//         {
//             int ans=0;
//             ans=max(ans,yes(idx+1,mask,dp,n,requests));
//             int newmask=(mask|(1<<idx));
//             ans=max(ans,1+yes(idx+1,newmask,dp,n,requests));
//             return ans;
//         }
//     }
//     int maximumRequests(int n, vector<vector<int>>& requests) {
//         int nn=requests.size();
//         int mask=(1<<nn);
//         cout<<nn<<endl;
//         vector<int>dp(mask,-1);
//         int ans=yes(0,0,dp,n,requests);
//         return ans;
//     }
// };



// // class Solution {
// // public:
// //     int ans=0;
// //     void yes(int idx,int n,vector<vector<int>>& requests,vector<int>& req,int count)
// //     {
// //         if(idx==requests.size())
// //         {
// //           for(int i=0;i<n;i++)
// //           {
// //               if(req[i])
// //               {
// //                   return;
// //               }
// //           }
// //           ans=max(ans,count);
// //         }
// //         else
// //         {
// //             req[requests[idx][0]]--;
// //             req[requests[idx][1]]++;
// //             yes(idx+1,n,requests,req,count+1);
// //             req[requests[idx][0]]++;
// //             req[requests[idx][1]]--;
// //             yes(idx+1,n,requests,req,count);

            

// //         }
// //     }
// //     int maximumRequests(int n, vector<vector<int>>& requests) {
// //         int nn=requests.size();
// //         vector<int>req(n,0);
// //         yes(0,n,requests,req,0);
// //         return ans;
// //     }
// // };
247000