其他分享
首页 > 其他分享> > D Array Differentiation

D Array Differentiation

作者:互联网

假设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;

标签:cnt,int,sum,dfs,ans,Differentiation,fff,Array
来源: https://blog.csdn.net/luker6/article/details/120505065