错误原因
没有写
错误更正
使用深度优先搜索,每次将每条边添加一条加上之后不超过总长除 的边
正确代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
int n;
int a[21];
int box[5];
bool flag=false;
void dfs(int x){
if(flag)return;
if(x-1==n){
if(box[1]==0&&box[2]==0&&box[3]==0&&box[4]==0){
flag=true;
return;
}
return ;
}
for(int i=1;i<=4;i++){
if(box[i]-a[x]>=0){
box[i]-=a[x];
dfs(x+1);
box[i]+=a[x];
}
}
return ;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
int sum=0;
flag = 0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
sum+=a[i];
}
if(sum%4!=0)cout<<"no\n";
else{
box[1]=sum/4;box[2]=sum/4;box[3]=sum/4;box[4]=sum/4;
dfs(1);
if(flag)cout<<"yes\n";
else cout<<"no\n";
}
}
return 0;
}