結果
| 問題 | No.1221 木 *= 3 |
| コンテスト | |
| ユーザー |
chocono2230
|
| 提出日時 | 2020-09-04 23:28:14 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,088 bytes |
| 記録 | |
| コンパイル時間 | 3,098 ms |
| コンパイル使用メモリ | 203,672 KB |
| 最終ジャッジ日時 | 2025-01-14 06:28:02 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 8 WA * 10 |
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;
void dfs(vector<vector<int>> &tree, vector<int> &b, vector<ll> &po, int now, int bf){
po.at(now) = (ll)tree.at(now).size() * b.at(now);
for(auto i : tree.at(now)){
po.at(now) += b.at(i);
if(i == bf) continue;
dfs(tree, b, po, i, now);
}
}
void fc(vector<vector<int>> &tree, vector<int> &b, vector<ll> &po, vector<bool> &chk, vector<int> &a, int now){
chk.at(now) = true;
for(auto i : tree.at(now)){
if(chk.at(i) == true) continue;
po.at(i) -= b.at(i);
if(po.at(i) < a.at(i)){
fc(tree, b, po, chk, a, i);
}
}
}
void dfs2(vector<vector<int>> &tree, vector<int> &a, vector<int> &b, vector<bool> &chk, int now, int bf, ll &ans){
ll tmp = 0;
if(chk.at(now) == true){
ans += a.at(now);
tmp += a.at(now);
}
for(auto i : tree.at(now)){
if(chk.at(now) == false && chk.at(i) == false){
ans += b.at(now);
tmp += b.at(now);
}
if(i == bf) continue;
dfs2(tree, a, b, chk, i, now, ans);
}
// cerr << now << " " << tmp << endl;
}
int main(){
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a.at(i);
vector<int> b(n);
rep(i, n) cin >> b.at(i);
vector<vector<int>> tree(n, vector<int>());
rep(i, n-1){
int a, b;
cin >> a >> b;
a--; b--;
tree.at(a).push_back(b);
tree.at(b).push_back(a);
}
vector<bool> chk(n, false);
vector<ll> po(n, 0);
dfs(tree, b, po, 0, -1);
rep(i, n){
if(chk.at(i) == true) continue;
if(po.at(i) < a.at(i)){
fc(tree, b, po, chk, a, i);
}
}
ll ans = 0;
dfs2(tree, a, b, chk, 0, -1, ans);
cout << ans << endl;
return 0;
}
chocono2230