結果

問題 No.1524 Upward Mobility
ユーザー 沙耶花沙耶花
提出日時 2021-05-27 22:09:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,789 bytes
コンパイル時間 1,357 ms
コンパイル使用メモリ 105,108 KB
実行使用メモリ 817,620 KB
最終ジャッジ日時 2024-04-24 08:02:29
合計ジャッジ時間 6,864 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 232 ms
67,660 KB
testcase_01 AC 189 ms
50,124 KB
testcase_02 AC 205 ms
54,604 KB
testcase_03 MLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>

using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int N;
vector<vector<int>> E;
vector<int> P;
vector<int> D;
vector<int> I;
vector<int> A;
vector<ll> B;
vector<map<int,ll>> dp;

void append_root(map<int,ll>& dest, int p, ll v){
  dest[p] += v;
  auto itr = dest.upper_bound(p);
  while(true){
    if(itr == dest.end()) return;
    if(itr->second > v){
      itr->second -= v;
      return;
    }
    v -= itr->second;
    itr = dest.erase(itr);
  }
}

void merge_tree(map<int,ll>& dest, const map<int,ll>& src){
  for(auto i : src) dest[i.first] += i.second;
}

int main(){
  cin >> N;
  E.resize(N);
  P.assign(N,-1);
  D.assign(N,0);
  for(int i=1; i<N; i++){
    int p; cin >> p; p--;
    E[p].push_back(i);
    P[i] = p;
    D[i] = D[p] + 1;
  }
  A.resize(N);
  rep(i,N){ cin >> A[i]; A[i] = N - A[i]; }
  B.resize(N);
  rep(i,N){ cin >> B[i]; }
  
  I = {0};
  for(int i=0; i<I.size(); i++) for(int e : E[I[i]]) I.push_back(e);
  reverse(I.begin(),I.end());

  dp.resize(N);
  for(int p : I){
    if(E[p].size() != 0){
      vector<pair<int,int>> childs;
      for(int e : E[p]) childs.push_back({ (int)dp[e].size() , e });
      //sort(childs.begin(),childs.end());
      reverse(childs.begin(),childs.end());
      for(int i=1; i<childs.size(); i++) merge_tree(dp[childs[0].second],dp[childs[i].second]);
      dp[p] = move(dp[childs[0].second]);
    }
    append_root(dp[p],A[p],B[p]);
  }
  
  ll ans = 0;
  for(auto p : dp[0]) ans += p.second;
  cout << ans << "\n";

  return 0;
}


struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_instance;

0