QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#713116 | #9580. 插排串联 | shift# | WA | 0ms | 3820kb | C++20 | 1.5kb | 2024-11-05 18:08:55 | 2024-11-05 18:08:55 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
void solve() {
int n;
std::cin >> n;
n += 1;
std::vector<int> r(n);
r[0] = 2200;
std::vector<std::vector<int>> adj(n);
for(int i = 1; i < n; i ++ ) {
int f, v;
std::cin >> f >> v;
adj[f].push_back(i);
r[i] = v;
}
std::vector<int> w(n);
auto dfs = [&](auto &self, int x, int p) -> void {
if(not adj[x].size()) {
w[x] = r[x];
return;
}
for(auto y : adj[x]) {
if(y == p) continue;
self(self, y, x);
w[x] += w[y];
}
};
dfs(dfs, 0, -1);
std::set<int> s;
for(int i = 1; i < n; i ++ ) {
if(r[i] < w[i]) {
s.insert(i);
}
}
if(s.size() == 0) {
std::cout << "YES" << '\n';
return;
}
if(s.size() > 1) {
std::cout << "NO" << '\n';
return;
}
int p = *s.begin();
bool ok = false;
for(int i = 1; i < n; i ++ ) {
if(r[i] > w[i]) {
if(r[p] >= w[i] and r[i] >= w[p]) {
ok = true;
}
}
}
std::cout << (ok ? "YES" : "NO") << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T = 1;
// std::cin >> T;
while(T -- ) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3556kb
input:
5 0 500 1 700 1 400 2 100 2 200
output:
YES
result:
ok single line: 'YES'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
5 0 500 1 700 1 400 2 100 2 300
output:
NO
result:
ok single line: 'NO'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3500kb
input:
4 0 1000 1 800 1 500 2 300
output:
YES
result:
ok single line: 'YES'
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3660kb
input:
3 0 1000000000 0 1000000000 0 147483647
output:
YES
result:
wrong answer 1st lines differ - expected: 'NO', found: 'YES'