QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#713131#9580. 插排串联shift#WA 1ms3760kbC++201.6kb2024-11-05 18:14:282024-11-05 18:14:28

Judging History

你现在查看的是最新测评结果

  • [2024-11-05 18:14:28]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3760kb
  • [2024-11-05 18:14:28]
  • 提交

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];
            r[x] = -1;
            return;
        }
        for(auto y : adj[x]) {
            if(y == p) continue;
            self(self, y, x);
            w[x] += w[y];
        }

    };

    dfs(dfs, 0, -1);

    if(r[0] < w[0]) {
        std::cout << "NO" << '\n';
        return;
    }
    
    int p = -1;
    for(int i = 1; i < n; i ++ ) {
        if(r[i] == -1) continue;
        if(r[i] < w[i]) {
            if(p != -1) {
                std::cout << "NO" << '\n';
                return;
            }
            p = i;
        }
    }

    if(p == -1) {
        std::cout << "YES" << '\n';
        return;
    }

    bool ok = false;
    for(int i = 1; i < n; i ++ ) {
        if(r[i] == -1) continue;
        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: 1ms
memory: 3752kb

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: 3756kb

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: 3760kb

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: 3592kb

input:

3
0 1000000000
0 1000000000
0 147483647

output:

NO

result:

ok single line: 'NO'

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 3620kb

input:

3
0 1000000000
0 1000000000
0 147483648

output:

YES

result:

wrong answer 1st lines differ - expected: 'NO', found: 'YES'