看数据范围这么小就想到状态压缩了,枚举2^16种方案,判断是否满足条件,取其中1数量最多的方案
class Solution {
public:
int count(int x) {
int ans = 0;
while(x) {
if(x % 2) ans++;
x /= 2;
}
return ans;
}
int maximumRequests(int n, vector<vector<int>>& requests) {
int rn = requests.size();
int ans = 0;
for(int i = 1; i < 1 << rn; i++) {
int satis = count(i);
if(satis < ans) continue;
vector<int> state(n, 0);
for(int j = 0; j < rn; j++) {
if(i >> j & 1) {
state[requests[j][0]]--;
state[requests[j][1]]++;
}
}
int flag = 1;
for(auto s : state) {
if(s != 0) {
flag = 0;
break;
}
}
if(flag) ans = max(ans, satis);
}
return ans;
}
};