Submission #1093045


Source Code Expand

//MIPT Cryptozoology
//Room 265
#define _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:128000000")
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <queue>
#include <deque>
#include <cmath>
#include <ctime>
#include <stack>
#include <bitset>
#include <set>
#include <map>
#include <cassert>
#include <memory.h>
#include <sstream>

using namespace std;

#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define itn int

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)

typedef long long li;
typedef long long i64;
typedef long double ld;
typedef vector<int> vi;
typedef pair <int, int> pi;

void solve(bool);

void precalc();

int TESTNUM = 0;
#define FILENAME ""

int main() {
	string s = FILENAME;
#ifdef YA
	//assert(!s.empty());
	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//cerr<<FILENAME<<endl;
	//assert (s != "change me please");
	clock_t start = clock();
#else
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//freopen(FILENAME ".in", "r", stdin);
	//freopen(FILENAME ".out", "w", stdout);
	cin.tie(0);
#endif
	cout.sync_with_stdio(0);
	cout.precision(10);
	cout << fixed;
	precalc();
	int t = 1;
	//cin >> t;
	int test_num = 1;
	while (t--) {
		//cout << "Case #" << test_num++ << ": ";
		++TESTNUM;
		solve(true);
	}
#ifdef YAH
	while (true) {
		solve(false);
	}
#endif
#ifdef YA
	cerr << "\n\n\n" << (clock() - start) / 1.0 / CLOCKS_PER_SEC << "\n\n\n";
#endif
	return 0;
}

//#define int li

/*int pr[] = { 97, 2011 };
int mods[] = { 1000000007, 1000000009 };

const int C = 100500;
int powers[2][C];*/

//int MOD = 1000000007;

//int c[5010][5010];

template<typename T>
T binpow(T q, T w, T mod) {
	if (!w)
		return 1 % mod;
	if (w & 1)
		return q * 1LL * binpow(q, w - 1, mod) % mod;
	return binpow(q * 1LL * q % mod, w / 2, mod);
}

void precalc() {

	/*for (int w = 0; w < 2; ++w) {
	powers[w][0] = 1;
	for (int j = 1; j < C; ++j) {
	powers[w][j] = (powers[w][j - 1] * 1LL * pr[w]) % mods[w];
	}
	}*/

	/*for (int i = 0; i < 5010; ++i) {
	c[i][i] = c[i][0] = 1;
	for (int j = 1; j < i; ++j) {
	c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD;
	}
	}*/
}

template<typename T>
T gcd(T q, T w) {
	while (w) {
		q %= w;
		swap(q, w);
	}
	return q;
}
template<typename T>
T lcm(T q, T w) {
	return q / gcd(q, w) * w;
}

inline int nxt(){
	int x;
	cin >> x;
	return x;
}

#define int li

vector<int> a;
vector<vector<int>> g;

bool ANS = true;

int dfs(int v, int p) {
	vector<int> nex;
	for (int to : g[v]) {
		if (to == p) {
			continue;
		}
		nex.push_back(dfs(to, v));
	}
	if (nex.empty()) {
		return a[v];
	}
	int sum = 0, mx = 0;
	for (int x : nex) {
		sum += x;
		mx = max(mx, x);
	}
	if (sum < a[v]) {
		ANS = false;
		return 0;
	}
	int paired = sum - a[v];
	if (sum - mx < paired) {
		ANS = false;
		return 0;
	}
	if (paired > a[v]) {
		ANS = false;
		return 0;
	}
	return a[v] - paired;
}

void solve(bool read) {
	int n;
	cin >> n;
	a.resize(n);
	g.resize(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	for (int i = 1; i < n; ++i) {
		int c, b;
		cin >> c >> b;
		--c; --b;
		g[c].push_back(b);
		g[b].push_back(c);
	}
	if (n == 1) {
		if (a[0]) {
			cout << "NO\n";
		}
		else {
			cout << "YES\n";
		}
		return;
	}
	if (n == 2) {
		if (a[0] == a[1]) {
			cout << "YES\n";
		}
		else {
			cout << "NO\n";
		}
		return;
	}

	int root = 0;
	while (g[root].size() == 1) {
		++root;
	}

	int rest = dfs(root, root);
	if (rest == 0 && ANS) {
		cout << "YES\n";
	}
	else {
		cout << "NO\n";
	}

}

Submission Info

Submission Time
Task C - Cleaning
User Kostroma
Language C++14 (GCC 5.4.1)
Score 700
Code Size 3831 Byte
Status AC
Exec Time 60 ms
Memory 14464 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 48
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
Case Name Status Exec Time Memory
in1.txt AC 54 ms 7168 KB
in10.txt AC 55 ms 7168 KB
in11.txt AC 59 ms 13056 KB
in12.txt AC 60 ms 13056 KB
in13.txt AC 57 ms 14464 KB
in14.txt AC 56 ms 13440 KB
in15.txt AC 3 ms 256 KB
in16.txt AC 2 ms 256 KB
in17.txt AC 3 ms 256 KB
in18.txt AC 3 ms 256 KB
in19.txt AC 55 ms 7168 KB
in2.txt AC 54 ms 7168 KB
in20.txt AC 55 ms 7168 KB
in21.txt AC 58 ms 7168 KB
in22.txt AC 54 ms 7168 KB
in23.txt AC 54 ms 7168 KB
in24.txt AC 54 ms 7168 KB
in25.txt AC 3 ms 256 KB
in26.txt AC 57 ms 7168 KB
in27.txt AC 57 ms 7168 KB
in28.txt AC 3 ms 256 KB
in29.txt AC 52 ms 7168 KB
in3.txt AC 55 ms 7168 KB
in30.txt AC 2 ms 256 KB
in31.txt AC 54 ms 7168 KB
in32.txt AC 54 ms 7168 KB
in33.txt AC 54 ms 7168 KB
in34.txt AC 54 ms 7168 KB
in35.txt AC 54 ms 7168 KB
in36.txt AC 54 ms 7168 KB
in37.txt AC 3 ms 384 KB
in38.txt AC 54 ms 7168 KB
in39.txt AC 54 ms 7168 KB
in4.txt AC 55 ms 7168 KB
in40.txt AC 54 ms 7168 KB
in41.txt AC 55 ms 7168 KB
in42.txt AC 54 ms 7168 KB
in43.txt AC 55 ms 7168 KB
in44.txt AC 54 ms 7168 KB
in45.txt AC 55 ms 7168 KB
in5.txt AC 54 ms 7168 KB
in6.txt AC 54 ms 7168 KB
in7.txt AC 49 ms 6400 KB
in8.txt AC 14 ms 2048 KB
in9.txt AC 55 ms 7168 KB
sample1.txt AC 3 ms 256 KB
sample2.txt AC 3 ms 256 KB
sample3.txt AC 3 ms 256 KB