結果

問題 No.1221 木 *= 3
ユーザー nawawannawawan
提出日時 2020-09-04 22:51:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,382 bytes
コンパイル時間 863 ms
コンパイル使用メモリ 81,756 KB
実行使用メモリ 16,820 KB
最終ジャッジ日時 2024-05-05 03:25:31
合計ジャッジ時間 3,245 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 2 ms
5,376 KB
testcase_07 WA -
testcase_08 AC 60 ms
12,288 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 51 ms
12,716 KB
testcase_14 AC 60 ms
13,840 KB
testcase_15 AC 58 ms
13,352 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 58 ms
12,288 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
typedef pair<long long, int> P;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<long long> a(N), b(N);
    for(int i = 0; i < N; i++) cin >> a[i];
    for(int i = 0; i < N; i++) cin >> b[i];
    vector<int> G[N];
    for(int i = 0; i < N - 1; i++){
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector<long long> size(N);
    for(int i = 0; i < N; i++){
        size[i] = G[i].size();
    }
    vector<int> used(N, 0);
    vector<long long> score(N, 0);
    long long sum = 0;
    for(int i = 0; i < N; i++){
        score[i] = size[i] * b[i];
        sum += score[i];
        for(int j: G[i]) score[i] += b[j];
    }
    priority_queue<P> pq;
    for(int i = 0; i < N; i++){
        score[i] = a[i] - score[i];
        if(score[i] > 0) pq.push(P(score[i], i));
    }
    while(!pq.empty()){
        P v = pq.top();
        pq.pop();
        long long temp = v.first;
        int ind = v.second;
        if(used[ind] == 1) continue;
        used[ind] = 1;
        sum += temp;
        for(int j: G[ind]){
            score[j] += b[j];
            if(score[j] > 0 && used[j] == 0) pq.push(P(score[j], j));
        }
    }
    cout << sum << endl;
}
0