Submission #19415770


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;

vector<vector<int>> graph;
vector<int> x, y;

void DFS(int v){
    vector<int> dp(x[v]+1, INF);
    dp[0] = 0;

    for(auto child_v : graph[v]){
        DFS(child_v);
        vector<int> dp_new(x[v]+1, INF);
        for(int sum=0; sum<=x[v]; sum++){
            if(sum + x[child_v] <= x[v]) dp_new[sum + x[child_v]] = min(dp_new[sum + x[child_v]], dp[sum] + y[child_v]);
            if(sum + y[child_v] <= x[v]) dp_new[sum + y[child_v]] = min(dp_new[sum + y[child_v]], dp[sum] + x[child_v]);
        }
        dp = dp_new;
    }
    for(int sum=0; sum<=x[v]; sum++){
        y[v] = min(y[v], dp[sum]);
    }
}

int main(){
    int n;
    cin >> n;

    graph.resize(n);
    for(int i=1; i<n; i++){
        int p;
        cin >> p;

        p -= 1;
        graph[p].push_back(i);
    }

    x.resize(n);
    y.assign(n, INF);
    for(int i=0; i<n; i++){
        cin >> x[i];
    }

    DFS(0);
    if(y[0] == INF) cout << "IMPOSSIBLE" << endl;
    if(y[0] != INF) cout << "POSSIBLE" << endl;
    return 0;
}

Submission Info

Submission Time
Task E - Bichrome Tree
User probably
Language C++ (GCC 9.2.1)
Score 700
Code Size 1123 Byte
Status AC
Exec Time 26 ms
Memory 8736 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 4
AC × 44
Set Name Test Cases
Sample subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt
Case Name Status Exec Time Memory
01.txt AC 6 ms 3476 KB
02.txt AC 23 ms 3576 KB
03.txt AC 24 ms 8736 KB
04.txt AC 4 ms 3604 KB
05.txt AC 6 ms 3452 KB
06.txt AC 9 ms 3632 KB
07.txt AC 7 ms 3588 KB
08.txt AC 15 ms 3612 KB
09.txt AC 15 ms 3524 KB
10.txt AC 26 ms 6984 KB
11.txt AC 15 ms 3592 KB
12.txt AC 8 ms 3620 KB
13.txt AC 12 ms 4588 KB
14.txt AC 3 ms 3424 KB
15.txt AC 7 ms 4688 KB
16.txt AC 3 ms 3668 KB
17.txt AC 20 ms 4708 KB
18.txt AC 7 ms 4708 KB
19.txt AC 4 ms 3588 KB
20.txt AC 5 ms 3648 KB
21.txt AC 17 ms 3608 KB
22.txt AC 19 ms 6852 KB
23.txt AC 14 ms 3500 KB
24.txt AC 15 ms 3568 KB
25.txt AC 12 ms 3584 KB
26.txt AC 5 ms 4544 KB
27.txt AC 7 ms 4608 KB
28.txt AC 10 ms 4640 KB
29.txt AC 7 ms 4616 KB
30.txt AC 9 ms 4752 KB
31.txt AC 7 ms 4768 KB
32.txt AC 5 ms 4708 KB
33.txt AC 7 ms 4592 KB
34.txt AC 9 ms 4732 KB
35.txt AC 8 ms 4596 KB
36.txt AC 8 ms 4740 KB
37.txt AC 13 ms 4712 KB
38.txt AC 7 ms 4668 KB
39.txt AC 12 ms 4620 KB
40.txt AC 11 ms 4764 KB
subtask0_0.txt AC 4 ms 3568 KB
subtask0_1.txt AC 2 ms 3376 KB
subtask0_2.txt AC 2 ms 3568 KB
subtask0_3.txt AC 2 ms 3588 KB