結果
問題 | No.1524 Upward Mobility |
ユーザー | 👑 Nachia |
提出日時 | 2021-05-13 23:30:12 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 233 ms / 6,000 ms |
コード長 | 1,787 bytes |
コンパイル時間 | 1,359 ms |
コンパイル使用メモリ | 104,104 KB |
最終ジャッジ日時 | 2025-01-21 10:45:38 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 31 |
ソースコード
#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;