<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{
   struct Edge{
      int v, w, sw, rev;
      constexpr Edge(int v, int w, int sw, int rev) noexcept: v(v), w(w), sw(sw), rev(rev){}
   };
public:
   int maximumRequests(int n, vector<vector<int>> &req) const{
      vector<vector<Edge>> g(n);
      int ans = 0;
      for(auto const &e: req){
         int u = e[0], v = e[1];
         if(u == v){
            ++ans; continue;
         }
         g[u].emplace_back(v, 1, 1, g[v].size());
         g[v].emplace_back(u, -1, 0, g[u].size()-1);
      }
      while(1){
         vector<int> d(n), pv(n), pe(n), cnt(n);
         queue<int> bf;
         for(int i=0; i<n; ++i){
            bf.push(i);
         }
         vector<bool> inq(n, true);
         int ncv = -1;
         while(!bf.empty()){
            int u = bf.front(); bf.pop(); inq[u] = false;
            for(size_t i=0; i<g[u].size(); ++i){
               auto [v, w, sw, _] = g[u][i];
               if(!sw || d[v]>=d[u]+w) continue;
               d[v] = d[u]+w;
               pv[v] = u; pe[v] = i;
               if(!inq[v]){
                  if(++cnt[v] == n){
                     ncv = v;
                     goto after_bf;
                  }
                  bf.push(v); inq[v] = true;
               }
            }
         }
         after_bf:
         if(ncv == -1) break;
         vector<bool> vis(n);
         for(; !vis[ncv]; ncv=pv[ncv]){
            vis[ncv] = true;
         }
         int u = ncv;
         do{
            int p = pv[u], i = pe[u];
            ans += g[p][i].w;
            g[p][i].sw = 0;
            g[u][g[p][i].rev].sw = 1;
            u = p;
         }while(u != ncv);
      }
      return ans;
   }
};
11900