結果

問題 No.1524 Upward Mobility
ユーザー 👑 NachiaNachia
提出日時 2021-05-13 23:30:12
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 203 ms / 6,000 ms
コード長 1,787 bytes
コンパイル時間 1,447 ms
コンパイル使用メモリ 108,036 KB
実行使用メモリ 65,112 KB
最終ジャッジ日時 2024-04-17 07:17:34
合計ジャッジ時間 6,257 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 198 ms
65,112 KB
testcase_01 AC 153 ms
47,448 KB
testcase_02 AC 178 ms
51,700 KB
testcase_03 AC 110 ms
20,792 KB
testcase_04 AC 196 ms
39,096 KB
testcase_05 AC 184 ms
39,276 KB
testcase_06 AC 174 ms
39,524 KB
testcase_07 AC 178 ms
38,844 KB
testcase_08 AC 174 ms
38,640 KB
testcase_09 AC 178 ms
39,000 KB
testcase_10 AC 203 ms
46,420 KB
testcase_11 AC 96 ms
21,968 KB
testcase_12 AC 58 ms
15,904 KB
testcase_13 AC 74 ms
15,968 KB
testcase_14 AC 82 ms
20,936 KB
testcase_15 AC 101 ms
21,072 KB
testcase_16 AC 171 ms
38,472 KB
testcase_17 AC 142 ms
33,136 KB
testcase_18 AC 35 ms
10,624 KB
testcase_19 AC 122 ms
30,108 KB
testcase_20 AC 44 ms
13,056 KB
testcase_21 AC 49 ms
14,592 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 80 ms
26,112 KB
testcase_30 AC 81 ms
26,112 KB
testcase_31 AC 98 ms
26,116 KB
testcase_32 AC 175 ms
51,120 KB
権限があれば一括ダウンロードができます

ソースコード

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