Problem:


Score : 700 points

Problem Statement

There is a tree with N vertices, numbered 1 through N.
The i-th of the N-1 edges connects vertices a_i and b_i.

Currently, there are A_i stones placed on vertex i.
Determine whether it is possible to remove all the stones from the vertices by repeatedly performing the following operation:

Select a pair of different leaves. Then, remove exactly one stone from every vertex on the path between those two vertices.
  Here, a leaf is a vertex of the tree whose degree is 1, and the selected leaves themselves are also considered as vertices on the path connecting them.

Note that the operation cannot be performed if there is a vertex with no stone on the path.

Constraints

2 ≦ N ≦ 10^5

1 ≦ a_i,b_i ≦ N

0 ≦ A_i ≦ 10^9

The given graph is a tree.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

If it is possible to remove all the stones from the vertices, print YES. Otherwise, print NO.

Sample Input 1

5
1 2 1 1 2
2 4
5 2
3 2
1 3

Sample Output 1

YES

All the stones can be removed, as follows:

Select vertices 4 and 5. Then, there is one stone remaining on each vertex except 4.

Select vertices 1 and 5. Then, there is no stone on any vertex.

Sample Input 2

3
1 2 1
1 2
2 3

Sample Output 2

NO

Sample Input 3

6
3 2 2 2 2 2
1 2
2 3
1 4
1 5
4 6

Sample Output 3

YES



Sample input:
5
1 2 1 1 2
2 4
5 2
3 2
1 3


Sample output:
YES

Submission status: Runtime Error

Solution code:
import copy

def traverse(N,A,mm):
	m=copy.deepcopy(mm)
	t=[]
	rp=[]
	c=[]
	for i in range(N+1):
		t.extend([0])
		rp.extend([0])
		c.extend([[0]])
	
	start=1
	for i in range(start,N+1):
		#if len(m[i])==1 and A[i]>0:
		if A[i]>0:
			root=i
			break
	
	eerror=False
	if len(m[root]) in [0,1]:
		t[root]=A[root]
	else:
		t[root]=2*A[root]
	
	
	while not(i==root and len(m[i])==0):
		#print(i,t[i],m[i],rp[i])
		
		if len(m[i])==0:
			#print('back')
			c[i][0]=t[i]
			r=i
			i=rp[i]
			#print(i,t[i],m[i],rp[i])
			t[i]-=t[r]
			c[i].extend([t[r]])
			#print('minus')
			#print(c)
			if t[i]<0:
				eerror=True
				break
		else:
			#print('forward')
			r=i
			i=m[i].pop()
			if rp[i]==0:
				rp[i]=r
			#print(i,t[i],m[i],rp[i])	
			if len(m[i]) in [0,1]:
				t[i]=A[i]
			else:
				t[i]=2*A[i]
			#print('plus')
			#print(i,t[i],m[i],rp[i])
			if r in m[i]:
				m[i].remove(r)
				#print('remove')
		
	
	for cc in c[1:]:
		cc_max=cc.index(max(cc))
		if len(cc)>1 and cc[cc_max]>sum([cc[i] for i in range(len(cc)) if i!=cc_max]):
			return(False)
			
	if t[root]==0 and eerror==False:
		return(True)
	else:
		return(False)
		
		
N= int(input())
A=[0]
for x in input().split():
	A.extend([int(x)])

m=[]

for i in range(N+1):
	m.extend([[]])

for i in range(N-1):
	l=[int(xx) for xx in input().split()]
	#l=[int(xx) for xx in L.pop()]
	m[l[0]]+=[l[1]]
	m[l[1]]+=[l[0]]

		
if traverse(N,A,m):
	print('YES')
else:
	print('NO')

Submission status: Runtime Error

Solution code:
N= int(input())
A=[[]]
for x in input().split():
	A.extend([int(x)])

m=[]
t=[]
rp=[]

for i in range(N+1):
	m.extend([[]])
	t.extend([0])
	rp.extend([0])

for i in range(N-1):
	l=[int(xx) for xx in input().split()]
	m[l[0]]+=[l[1]]
	m[l[1]]+=[l[0]]

for i in range(1,N+1):
	if len(m[i])==1 and A[i]>0:
		root=i
		break

eerror=False
t[root]=2*A[root]
r=root
i=m[root].pop()
rp[i]=r


if r in m[i]:
	m[i].remove(r)
if len(m[i])==0:
	t[i]=A[i]
else:
	t[i]=2*A[i]

while i!=root:
	#print(r,t[r],m[r],rp[r],'->',i,t[i],m[i],rp[i])
	
	if len(m[i])==0:
		r=i
		i=rp[i]
		t[i]-=t[r]
		if t[i]<0:
			eerror=True
			break
	else:
		r=i
		i=m[i].pop()
		if rp[i]==0:
			rp[i]=r
			
		if r in m[i]:
			m[i].remove(r)
		if len(m[i])==0:
			t[i]=A[i]
		else:
			t[i]=2*A[i]
			
#print(r,t[r],m[r],'->',i)					
if t[r]==A[root] and eerror==False:
	print('YES')
else:
	print('NO')

Submission status: Runtime Error

Solution code:
N= int(input())
A=[[]]
for x in input().split():
	A.extend([int(x)])

m=[]
t=[]
rp=[]

for i in range(N+1):
	m.extend([[]])
	t.extend([0])
	rp.extend([0])

for i in range(N-1):
	l=[int(xx) for xx in input().split()]
	m[l[0]]+=[l[1]]
	m[l[1]]+=[l[0]]

for i in range(1,N+1):
	if len(m[i])==1 and A[i]>0:
		root=i
		break

eerror=False
r=root
i=m[root].pop()
rp[i]=r
t[root]=2*A[i]

if r in m[i]:
	m[i].remove(r)
if len(m[i])==0:
	t[i]=A[i]
else:
	t[i]=2*A[i]

while i!=root:
	
	if len(m[i])==0:
		r=i
		i=rp[i]
		t[i]-=t[r]
		if t[i]<0:
			eerror=True
			break
	else:
		r=i
		i=m[i].pop()
		if rp[i]==0:
			rp[i]=r
			
		if r in m[i]:
			m[i].remove(r)
		if len(m[i])==0:
			t[i]=A[i]
		else:
			t[i]=2*A[i]
						
if t[r]==A[root] and eerror==False:
	print('YES')
else:
	print('NO')

Submission status: Runtime Error

Solution code:
def TTT(me=0,r=0):
	mm=m[me]
	l=len(mm)
	
	if l==1:
		if r!=0:
			ret=A[me]
		else:
			ret=TTT(mm[0],me)
	
	else:
		c=0
		for i in range(l):
			if mm[i]!=r:
				c+=TTT(mm[i],me)
		
		ret=2*A[me]-c
	
	#print(me,r,ret)
	return(ret)
			

N= int(input())

A=[[]]+[int(x) for x in input().split()]

L=[]
for i in range(N-1):
	L.extend([[int(xx)for xx in input().split()]])

m=[]
for i in range(N+1):
	m.extend([[]])
	
for l in L:
	m[l[0]]+=[l[1]]
	m[l[1]]+=[l[0]]
	
for i in range(N):
	if len(m[i])==1:
		break
root=i
		
Tvalue=TTT(m[root][0],root)
if Tvalue==A[root]:
	print('YES')
else:
	print('NO')

Submission status: Runtime Error

Solution code:
def TTT(A=[],m=[],me=0,r=0):
	mm=m[me]
	l=len(mm)
	
	if l==1:
		if r!=0:
			ret=A[me]
		else:
			ret=TTT(A,m,mm[0],me)
	
	else:
		c=0
		for i in range(l):
			if mm[i]!=r:
				c+=TTT(A,m,mm[i],me)
		
		ret=2*A[me]-c
	
	#print(me,r,ret)
	return(ret)
			

N= int(input())

A=[[]]+[int(x) for x in input().split()]

L=[]
for i in range(N-1):
	L.extend([[int(xx)for xx in input().split()]])

m=[]
for i in range(N+1):
	m.extend([[]])
	
for l in L:
	m[l[0]]+=[l[1]]
	m[l[1]]+=[l[0]]
	
for i in range(N):
	if len(m[i])==1:
		break
root=i
		
Tvalue=TTT(A,m,m[root][0],root)
if Tvalue==A[root]:
	print('YES')
else:
	print('NO')

Submission status: Runtime Error

Solution code:
def T(A=[],lines=[],me=0,parent=0):
	if [x for y in lines for x in y].count(me)==1 and parent==0:
		for a in lines:
			if a[0]==me:
				return(T(A,lines,a[1],me))
			elif a[1]==me:
				return(T(A,lines,a[0],me))
	elif [me,parent] not in lines and [parent,me] not in lines:
		return(0)
	else:
		sl=0
		for a in lines:
			if a[0]==me and a[1]!=parent:
				sl=sl+T(A,lines,a[1],me)
			elif a[1]==me and a[0]!=parent:
				sl=sl+T(A,lines,a[0],me)
		
		if sl==0:
			return(A[me-1])
		else:
			return(A[me-1]*2-sl)

N= Int(input())

A=[int(x) for x in input().split()]

L=[]
for i in range(N-1):
   L=L+[int(xx)for xx in input().split()]
LF=[x for y in L for x in y]
LF1= [x for x in LF if LF.count(x)==1]

if T(A,L,min(LF1))==A[min(LF1)]-1:
	print('YES')
else:
	print('NO')