Submission #1372899
Source Code Expand
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <cassert> #include <iomanip> #include <iostream> #include <algorithm> #include <unordered_set> #include <unordered_map> using namespace std; #define f first #define s second #define pb push_back #define pp pop_back #define mp make_pair #define ll long long #define ld double #define ull unsigned long long #define PI pair < int, int > const int N = 5e5 + 123; const int M = 123; const ld Pi = acos(-1); const ll Inf = 1e18; const int inf = 1e9; const int mod = 1e9 + 7; const int Sz = 501; const int MOD = 1e9 + 7; void add(int &a, int b) { a += b; if (a >= mod) a -= mod; } int mult(int a, int b) { return 1ll * a * b % mod; } int sum(int a, int b) { add(a, b); return a; } int n, a[N]; vector < int > g[N]; ll dfs(int v, int p = -1) { vector < ll > now; ll x = a[v]; ll y = 0; for (auto to : g[v]) { if (to != p) { ll tmp = dfs(to, v); y += tmp; now.pb(tmp); } } if (now.size() == 0) return x; if (x > y) { cout << "NO\n"; exit(0); } ll z = y - x; multiset < ll > st; for (auto i : now) st.insert(i); while(z > 0) { if (now.size() < 2) break; ll mn = *st.begin(); st.erase(st.find(mn)); ll mx = *st.rbegin(); st.erase(st.find(mx)); ll cur = min(z, mn); mn -= cur; mx -= cur; x -= cur; y -= cur; z -= cur; if (mn > 0) st.insert(mn); if (mx > 0) st.insert(mx); } if (z > 0) { cout << "NO\n"; exit(0); } return x; } void solve() { scanf("%d", &n); for (int i = 1;i <= n;i++) scanf("%d", &a[i]); for (int i = 1, u, v;i < n;i++) { scanf("%d%d", &u, &v); g[u].pb(v); g[v].pb(u); } if (n == 2) { if (a[1] != a[2]) cout << "NO\n"; else cout << "YES\n"; return; } int rt = 1; while(g[rt].size() == 1) rt++; assert(rt <= n && g[rt].size() > 1); if (dfs(rt) == 0) cout << "YES\n"; else cout << "NO\n"; } int main() { #ifdef wws freopen("in", "r", stdin); // freopen("in", "w", stdout); #endif // ios_base::sync_with_stdio(0); int tt = 1; while(tt--) solve(); return 0; }
Submission Info
Submission Time | |
---|---|
Task | C - Cleaning |
User | SmallBoy |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 2547 Byte |
Status | RE |
Exec Time | 2104 ms |
Memory | 29056 KB |
Compile Error
./Main.cpp: In function ‘void solve()’: ./Main.cpp:104:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &n); ^ ./Main.cpp:105:48: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] for (int i = 1;i <= n;i++) scanf("%d", &a[i]); ^ ./Main.cpp:107:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d%d", &u, &v); ^
Judge Result
Set Name | Sample | All | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 700 | ||||||||||
Status |
|
|
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 | 63 ms | 15744 KB |
in10.txt | AC | 60 ms | 15744 KB |
in11.txt | AC | 71 ms | 26496 KB |
in12.txt | AC | 68 ms | 26496 KB |
in13.txt | AC | 66 ms | 29056 KB |
in14.txt | AC | 66 ms | 27136 KB |
in15.txt | RE | 101 ms | 12544 KB |
in16.txt | RE | 104 ms | 12544 KB |
in17.txt | AC | 5 ms | 12544 KB |
in18.txt | AC | 5 ms | 12544 KB |
in19.txt | RE | 149 ms | 15744 KB |
in2.txt | AC | 62 ms | 15744 KB |
in20.txt | RE | 141 ms | 15744 KB |
in21.txt | RE | 141 ms | 15872 KB |
in22.txt | RE | 151 ms | 15744 KB |
in23.txt | AC | 56 ms | 15744 KB |
in24.txt | AC | 52 ms | 15744 KB |
in25.txt | RE | 103 ms | 12544 KB |
in26.txt | AC | 49 ms | 15744 KB |
in27.txt | AC | 50 ms | 15744 KB |
in28.txt | TLE | 2103 ms | 12544 KB |
in29.txt | RE | 138 ms | 15744 KB |
in3.txt | RE | 153 ms | 15744 KB |
in30.txt | AC | 5 ms | 12544 KB |
in31.txt | AC | 57 ms | 15744 KB |
in32.txt | AC | 51 ms | 15744 KB |
in33.txt | RE | 154 ms | 15744 KB |
in34.txt | RE | 154 ms | 15744 KB |
in35.txt | AC | 61 ms | 15744 KB |
in36.txt | AC | 60 ms | 15744 KB |
in37.txt | RE | 103 ms | 12544 KB |
in38.txt | RE | 150 ms | 15744 KB |
in39.txt | RE | 151 ms | 15744 KB |
in4.txt | RE | 164 ms | 15744 KB |
in40.txt | RE | 151 ms | 15744 KB |
in41.txt | TLE | 2104 ms | 15744 KB |
in42.txt | RE | 154 ms | 15744 KB |
in43.txt | AC | 55 ms | 15744 KB |
in44.txt | RE | 156 ms | 15744 KB |
in45.txt | RE | 154 ms | 15744 KB |
in5.txt | AC | 63 ms | 15744 KB |
in6.txt | AC | 60 ms | 15744 KB |
in7.txt | RE | 148 ms | 15360 KB |
in8.txt | AC | 18 ms | 13312 KB |
in9.txt | AC | 61 ms | 15872 KB |
sample1.txt | AC | 5 ms | 12544 KB |
sample2.txt | AC | 5 ms | 12544 KB |
sample3.txt | RE | 102 ms | 12544 KB |