Submission #19416336


Source Code Expand

Copy
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
template<class T> inline int  sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
int main()
{
  int n; cin >> n;
  vi p(n-1);
  rep(i,n-1) {
    cin >> p[i];
    p[i]--;
  }
  vi x(n);
  rep(i,n) cin >> x[i];

  vvi edge(n);
  rep(i,n-1) {
    edge[p[i]].push_back(i+1);
  }

  auto merge = [&](map<int,int> a, map<int,int> b)->map<int,int> {
    map<int,int> res;
    for(auto atmp: a) {
      auto [akey, aval] = atmp;
      for(auto btmp: b) {
        auto [bkey, bval] = btmp;
        if(akey + bkey > 5000) continue;
        res[akey+bkey] = 1;
      }
    }
    return res;
  };

  vi d(n, 0);
  auto dfs = [&](auto self, int v)->map<int,int> {
    map<int,int> mp;
    mp[0] = 1;
    for(auto w: edge[v]) {
      mp = merge(mp, self(self, w));
      d[v] += d[w];
    }
    map<int,int> res;
    int mx = -1;
    for(auto tmp: mp) {
      auto [key, val] = tmp;
      if(key <= x[v]) {
        mx = key;
      }
    }
    if(d[v] - mx < 0 || mx == -1) {
      cout << "IMPOSSIBLE\n";
      exit(0);
    }
    if(d[v] - mx <= 5000) res[d[v] - mx] = 1;
    res[x[v]] = 1;
    d[v] = x[v] + d[v] - mx;
    return res;
  };

  dfs(dfs, 0);
  cout << "POSSIBLE\n";

  return 0;
}

Submission Info

Submission Time
Task E - Bichrome Tree
User kkktym
Language C++ (GCC 9.2.1)
Score 700
Code Size 2010 Byte
Status AC
Exec Time 146 ms
Memory 4372 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 146 ms 3884 KB
02.txt AC 4 ms 3588 KB
03.txt AC 4 ms 3756 KB
04.txt AC 53 ms 4292 KB
05.txt AC 48 ms 4200 KB
06.txt AC 62 ms 4344 KB
07.txt AC 53 ms 4312 KB
08.txt AC 43 ms 4148 KB
09.txt AC 39 ms 4372 KB
10.txt AC 3 ms 3612 KB
11.txt AC 3 ms 3640 KB
12.txt AC 38 ms 4316 KB
13.txt AC 5 ms 3736 KB
14.txt AC 141 ms 3968 KB
15.txt AC 4 ms 3756 KB
16.txt AC 4 ms 3632 KB
17.txt AC 4 ms 3664 KB
18.txt AC 3 ms 3628 KB
19.txt AC 14 ms 3652 KB
20.txt AC 37 ms 4040 KB
21.txt AC 4 ms 3616 KB
22.txt AC 2 ms 3704 KB
23.txt AC 21 ms 4316 KB
24.txt AC 17 ms 4044 KB
25.txt AC 33 ms 4252 KB
26.txt AC 4 ms 3680 KB
27.txt AC 4 ms 3884 KB
28.txt AC 7 ms 3728 KB
29.txt AC 5 ms 3856 KB
30.txt AC 6 ms 3884 KB
31.txt AC 4 ms 3696 KB
32.txt AC 5 ms 3880 KB
33.txt AC 3 ms 3524 KB
34.txt AC 3 ms 3628 KB
35.txt AC 2 ms 3616 KB
36.txt AC 2 ms 3616 KB
37.txt AC 3 ms 3668 KB
38.txt AC 3 ms 3696 KB
39.txt AC 4 ms 3640 KB
40.txt AC 3 ms 3596 KB
subtask0_0.txt AC 2 ms 3564 KB
subtask0_1.txt AC 2 ms 3500 KB
subtask0_2.txt AC 2 ms 3428 KB
subtask0_3.txt AC 2 ms 3496 KB