classSolution { private: vector<int> delta; int ans = 0, cnt = 0, zero, n;
public: voiddfs(vector<vector<int>> &requests, int pos){ if (pos == requests.size()) { if (zero == n) { ans = max(ans, cnt); } return; }
// 不选 requests[pos] dfs(requests, pos + 1);
// 选 requests[pos] int z = zero; ++cnt; auto &r = requests[pos]; int x = r[0], y = r[1]; zero -= delta[x] == 0; --delta[x]; zero += delta[x] == 0; zero -= delta[y] == 0; ++delta[y]; zero += delta[y] == 0; dfs(requests, pos + 1); --delta[y]; ++delta[x]; --cnt; zero = z; }
intmaximumRequests(int n, vector<vector<int>> &requests){ delta.resize(n); zero = n; this->n = n; dfs(requests, 0); return ans; } };
funcmaximumRequests(n int, requests [][]int) (ans int) { delta := make([]int, n) cnt, zero := 0, n var dfs func(int) dfs = func(pos int) { if pos == len(requests) { if zero == n && cnt > ans { ans = cnt } return }
// 不选 requests[pos] dfs(pos + 1)
// 选 requests[pos] z := zero cnt++ r := requests[pos] x, y := r[0], r[1] if delta[x] == 0 { zero-- } delta[x]-- if delta[x] == 0 { zero++ } if delta[y] == 0 { zero-- } delta[y]++ if delta[y] == 0 { zero++ } dfs(pos + 1) delta[y]-- delta[x]++ cnt-- zero = z } dfs(0) return }
voiddfs(constint** requests, int requestsSize,int n, int pos, int * delta, int * ans, int * cnt, int * zero) { if (pos == requestsSize) { if (*zero == n) { *ans = MAX(*ans, *cnt); } return; }
// 不选 requests[pos] dfs(requests, requestsSize, n, pos + 1, delta, ans, cnt, zero);
// 选 requests[pos] int z = *zero; ++*cnt; constint * r = requests[pos]; int x = r[0], y = r[1]; *zero -= delta[x] == 0; --delta[x]; *zero += delta[x] == 0; *zero -= delta[y] == 0; ++delta[y]; *zero += delta[y] == 0; dfs(requests, requestsSize, n, pos + 1, delta, ans, cnt, zero); --delta[y]; ++delta[x]; --*cnt; *zero = z; }
intmaximumRequests(int n, int** requests, int requestsSize, int* requestsColSize) { int * delta = (int *)malloc(sizeof(int) * n); memset(delta, 0, sizeof(int) * n); int ans = 0, cnt = 0, zero = n; dfs(requests, requestsSize, n, 0, delta, &ans, &cnt, &zero); return ans; }
publicclassSolution { publicintMaximumRequests(int n, int[][] requests) { int[] delta = newint[n]; int ans = 0, m = requests.Length; for (int mask = 0; mask < (1 << m); ++mask) { int cnt = BitCount(mask); if (cnt <= ans) { continue; } Array.Fill(delta, 0); for (int i = 0; i < m; ++i) { if ((mask & (1 << i)) != 0) { ++delta[requests[i][0]]; --delta[requests[i][1]]; } } bool flag = true; foreach (int x in delta) { if (x != 0) { flag = false; break; } } if (flag) { ans = cnt; } } return ans; }
privatestaticintBitCount(int i) { i = i - ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0f0f0f0f; i = i + (i >> 8); i = i + (i >> 16); return i & 0x3f; } }
funcmaximumRequests(n int, requests [][]int) (ans int) { next: for mask := 0; mask < 1<<len(requests); mask++ { cnt := bits.OnesCount(uint(mask)) if cnt <= ans { continue } delta := make([]int, n) for i, r := range requests { if mask>>i&1 == 1 { delta[r[0]]++ delta[r[1]]-- } } for _, d := range delta { if d != 0 { continue next } } ans = cnt } return }
intmaximumRequests(int n, int** requests, int requestsSize, int* requestsColSize) { int * delta = (int *)malloc(sizeof(int) * n); int ans = 0, m = requestsSize; for (int mask = 0; mask < (1 << m); ++mask) { int cnt = __builtin_popcount(mask); if (cnt <= ans) { continue; } memset(delta, 0, sizeof(int) * n); for (int i = 0; i < m; ++i) { if (mask & (1 << i)) { ++delta[requests[i][0]]; --delta[requests[i][1]]; } } bool flag = true; for (int i = 0; i < n; i++) { if (delta[i] != 0) { flag = false; break; } } if (flag) { ans = cnt; } } return ans; }
var maximumRequests = function(n, requests) { const delta = newArray(n).fill(0); let ans = 0, m = requests.length; for (let mask = 0; mask < (1 << m); ++mask) { let cnt = mask.toString(2).split('0').join('').length; if (cnt <= ans) { continue; } delta.fill(0); for (let i = 0; i < m; ++i) { if ((mask & (1 << i)) !== 0) { ++delta[requests[i][0]]; --delta[requests[i][1]]; } } let flag = true; for (const x of delta) { if (x !== 0) { flag = false; break; } } if (flag) { ans = cnt; } } return ans; };
复杂度分析
时间复杂度:O(2^m \times n),其中 m 是数组 requests 的长度,即请求的数量。从 m 个请求中任意选择请求的方案数为 2^m,对于每一种方案,我们需要 O(n) 的时间判断其是否满足要求。