时间限制
400 ms
内存限制
64 MB
思路 :
- 用原图的特征值得出所有合法的平均值,再依次判断每次的图中每个数
- dfs枚举有两种方式;第一种:将50个特征值枚举一遍,每次枚举选或者不选,等到选了四个return,这样是 4 50 4^{50} 450;第二种:枚举四个数分别选什么,这样是 5 0 4 50^4 504;因此,选后者
- 还要注意算出的平均值可能非整数,这种舍弃
#include <iostream>
using namespace std;
const int N = 55, INF = 1e6;
int n, k;
int a[N];
bool st[INF];
bool vis[N];
void dfs(int u, int sum) {
if (u == 5) {
if (sum % 4 == 0) {
st[sum / 4] = true;
}
return ;
}
for (int i = 1; i <= n; ++ i) {
if (!vis[i]) {
vis[i] = true;
dfs(u + 1, sum + a[i]);
vis[i] = false;
}
}
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
}
dfs(1, 0);
while (k -- ) {
int m;
cin >> m;
bool ok = true;
for (int i = 0; i < m; ++ i) {
int x;
cin >> x;
if (!st[x]) {
ok = false;
}
}
if (ok) cout << "Yes" << endl;
else cout << "No" << endl;
}
}