Language: C++

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <functional>
#include <iostream>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int a[100001];
vector<int> G[100001];
bool used[100001];
bool flag[100001];
int cnt[100001];
ll query[100001];
bool next[100001];

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n-1;i++){
		int f,t;
		scanf("%d%d",&f,&t);
		f--;
		t--;
		G[f].push_back(t);
		G[t].push_back(f);
	}
	queue<int> que;
	bool cont=true;
	for(int i=0;i<n;i++){
		if(G[i].size()==1){
			que.push(i);
			flag[i]=true;
			cnt[i]=1;
			query[i]=a[i];
		}
	}
	while(cont){
		queue<int> que2;
		cont=false;
		while(que.size()){
			int v=que.front();
			que.pop();
			if(used[v])continue;
			used[v]=true;
			if(cnt[v]==1){
				if(query[v]!=a[v]){
					printf("NO\n");
					return 0;
				}
			}else{
				if(query[v]<a[v] || a[v]*2<query[v]){
					printf("NO\n");
					return 0;
				}
				query[v]=a[v]*2LL-query[v];
			}
			if(query[v]==0)continue;
			for(int i=0;i<G[v].size();i++){
				if(!used[G[v][i]]){
					if(flag[G[v][i]]){
						ll nv=query[G[v][i]];
						if(cnt[G[v][i]]==1){
							if(nv!=a[G[v][i]]){
								printf("NO\n");
								return 0;
							}
						}else{
							if(nv<a[G[v][i]] || a[G[v][i]]*2<nv){
								printf("NO\n");
								return 0;
							}
							nv=a[G[v][i]]*2LL-query[G[v][i]];
						}
						if(query[v]!=nv){
							printf("NO\n");
							return 0;
						}else{
							used[G[v][i]]=true;
							query[v]=0;
							query[G[v][i]]=0;
						}
					}else{
						if(query[v]>a[v]){
							printf("NO\n");
							return 0;
						}
						cnt[G[v][i]]++;
						query[G[v][i]]+=query[v];
						query[v]=0;
						if(!next[G[v][i]] && cnt[G[v][i]]+1==G[G[v][i]].size()){
							que2.push(G[v][i]);
							next[G[v][i]]=true;
						}
					}
				}
			}
			if(query[v]!=0){
				printf("NO\n");
				return 0;
			}
		}
		memset(flag,false,sizeof(flag));
		while(que2.size()){
			cont=true;
			int v=que2.front();
			que2.pop();
			que.push(v);
			flag[v]=true;
		}
	}
	printf("YES\n");
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:103:53: error: reference to 'next' is ambiguous
  103 |                                                 if(!next[G[v][i]] && cnt[G[v][i]]+1==G[G[v][i]].size()){
      |                                                     ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/vector:62,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:25:6: note:                 'bool next [100001]'
   25 | bool next[100001];
      |      ^~~~
a.cc:105:57: error: reference to 'next' is ambiguous
  105 |                                                         next[G[v][i]]=true;
      |                                                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:25:6: note:                 'bool next [100001]'
   25 | bool next[100001];
      |      ^~~~