Submission #1460246


Source Code Expand

#include<bits/stdc++.h>
using namespace std;

long long N;
long long A[100005];
vector<long long> tree[100005];

//上に流れる分
long long dfs(long long pa, long long cur){

	//leaf
	if(tree[cur].size() == 1 && tree[cur][0] == pa){
		return A[cur];
	}

	long long child = 0;
	for(long long i = 0; i < tree[cur].size(); i++){
		if(pa == tree[cur][i]){
			continue;
		}
		long long tmp = dfs(cur, tree[cur][i]);
		if(tmp < 0){
			return -1;
		}
		child += tmp;
	}
	return (2 * A[cur] - child);
}

int main(){
	cin >> N;
	for(long long i = 0; i < N; i++){
		cin >> A[i];
	}
	for(long long i = 0; i < N - 1; i++){
		long long a,b;
		cin >> a >> b;
		a--;b--;
		tree[a].push_back(b);
		tree[b].push_back(a);
	}

	long long ret = dfs(-1, 0);
	if(ret < 0){
		cout << "NO" << endl;
		return 0;
	}

	if(tree[0].size() == 1){
		if(A[0] == ret){
			cout << "YES" << endl;
		}else{
			cout << "NO" << endl;
			return 1;
		}
	}else{
		if(0 == ret){
			cout << "YES" << endl;
		}else{
			cout << "NO" << endl;
		}
	}
}

Submission Info

Submission Time
Task C - Cleaning
User motomuman
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1073 Byte
Status RE
Exec Time 115 ms
Memory 11776 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 3
AC × 37
WA × 9
RE × 5
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
in1.txt AC 102 ms 7040 KB
in10.txt AC 102 ms 7040 KB
in11.txt AC 113 ms 10880 KB
in12.txt AC 113 ms 10880 KB
in13.txt AC 91 ms 11776 KB
in14.txt AC 90 ms 11136 KB
in15.txt AC 2 ms 2560 KB
in16.txt AC 2 ms 2560 KB
in17.txt AC 2 ms 2560 KB
in18.txt RE 2 ms 2560 KB
in19.txt WA 102 ms 7040 KB
in2.txt AC 103 ms 7168 KB
in20.txt WA 105 ms 7168 KB
in21.txt WA 105 ms 7040 KB
in22.txt WA 102 ms 7040 KB
in23.txt WA 104 ms 7168 KB
in24.txt RE 107 ms 7168 KB
in25.txt AC 2 ms 2560 KB
in26.txt AC 110 ms 7040 KB
in27.txt AC 110 ms 7168 KB
in28.txt AC 2 ms 2560 KB
in29.txt AC 87 ms 7168 KB
in3.txt AC 102 ms 7040 KB
in30.txt WA 2 ms 2560 KB
in31.txt WA 107 ms 7168 KB
in32.txt WA 115 ms 7168 KB
in33.txt AC 105 ms 7168 KB
in34.txt AC 104 ms 7168 KB
in35.txt AC 109 ms 7040 KB
in36.txt AC 104 ms 7168 KB
in37.txt AC 3 ms 2688 KB
in38.txt AC 101 ms 7168 KB
in39.txt AC 102 ms 7168 KB
in4.txt AC 103 ms 7168 KB
in40.txt AC 99 ms 7040 KB
in41.txt AC 100 ms 7168 KB
in42.txt RE 103 ms 7040 KB
in43.txt RE 103 ms 7040 KB
in44.txt WA 106 ms 7168 KB
in45.txt RE 103 ms 7040 KB
in5.txt AC 102 ms 7040 KB
in6.txt AC 103 ms 7168 KB
in7.txt AC 93 ms 6656 KB
in8.txt AC 25 ms 3584 KB
in9.txt AC 102 ms 7168 KB
sample1.txt AC 2 ms 2560 KB
sample2.txt AC 2 ms 2560 KB
sample3.txt AC 2 ms 2560 KB