QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#713104 | #9580. 插排串联 | shift# | WA | 1ms | 3832kb | C++20 | 1.3kb | 2024-11-05 18:05:38 | 2024-11-05 18:05:38 |
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);
int ned = -1, cnt = 0, ok = false;
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];
}
if(r[x] < w[x]) {
ned = x;
cnt += 1;
}
};
dfs(dfs, 0, -1);
for(int i = 1; i < n; i ++ ) {
if(r[i] >= w[i]) {
if(r[i] >= w[ned] and r[ned] >= w[i]) {
ok = true;
}
}
}
if(cnt == 1 and ok or cnt == 0) {
std::cout << "YES" << '\n';
} else {
std::cout << "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: 3560kb
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: 1ms
memory: 3772kb
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: 1ms
memory: 3604kb
input:
4 0 1000 1 800 1 500 2 300
output:
YES
result:
ok single line: 'YES'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
3 0 1000000000 0 1000000000 0 147483647
output:
NO
result:
ok single line: 'NO'
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 3488kb
input:
3 0 1000000000 0 1000000000 0 147483648
output:
YES
result:
wrong answer 1st lines differ - expected: 'NO', found: 'YES'