QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#713142#9580. 插排串联shift#WA 1ms3860kbC++201.6kb2024-11-05 18:16:362024-11-05 18:16:45

Judging History

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

  • [2024-11-05 18:16:45]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3860kb
  • [2024-11-05 18:16:36]
  • 提交

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 = 0; i < n; i ++ ) {
        if(r[i] == -1 or r[i] < w[i]) continue;
        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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3788kb

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

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

input:

4
0 1000
1 800
1 500
2 300

output:

YES

result:

ok single line: 'YES'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3600kb

input:

3
0 1000000000
0 1000000000
0 147483647

output:

NO

result:

ok single line: 'NO'

Test #5:

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

input:

3
0 1000000000
0 1000000000
0 147483648

output:

YES

result:

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