假设b[i] 为点权, a[i] 为边权,连成树,只有 n-1 条边,如果
其中有环,则有至少n条边。判断是否有环:其中的元素相加是否是0;
可以用dfs 或者 直接遍历
const int N=110;
int n, ans;
int a[N];
set<int> q;
void dfs(int x, int s)
{
if(x > n)
{
if(q.count(s)) ans = 1;
q.insert(s);
return ;
}
if(ans) return ;
dfs(x+1, s);
dfs(x+1, s+a[x]);
}
void solve()
{
ans = 0;
scanf("%d", &n);
rep(i,1,n)
scanf("%d",&a[i]);
q.clear();
dfs(1,0);
if(ans) puts("YES");
else puts("NO");
}
for (int i = 1; i <= n; i++)
cin >> v[i];
int kase = pow(3,n);
bool fff = 0;
for (int i = 1; i < kase; i++) {
int cnt = i;
int sum = 0;
for (int j = 1; j <= n; j++) {
if (cnt % 3 == 1)sum += v[j];
if (cnt % 3 == 2)sum -= v[j];
cnt /= 3;
}
if (sum == 0) {
fff = 1;
break;
}
}
if (fff)cout << "YES" << endl;
else cout << "NO" << endl;