結果

問題 No.386 貪欲な領主
ユーザー konsin_tokagekonsin_tokage
提出日時 2018-03-16 17:11:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,620 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 70,960 KB
実行使用メモリ 20,288 KB
最終ジャッジ日時 2023-08-23 11:50:44
合計ジャッジ時間 3,589 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 AC 268 ms
20,288 KB
testcase_06 AC 288 ms
19,908 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 35 ms
4,548 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 8 ms
4,380 KB
testcase_14 AC 281 ms
19,912 KB
testcase_15 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<vector<int>> graph(n);
    for(int i=1; i<n; ++i){
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    vector<int> u(n);
    for(int &i: u)
        cin >> i;
    int maxlog = 0;
    while((1<<maxlog) <= n) 
        ++maxlog;
    vector<vector<int>> parent(n, vector<int>(maxlog));
    vector<int> cost(u), depth(n);
    queue<int> que;
    que.push(0);
    while(!que.empty()){
        int i = que.front();
        que.pop();
        for(int j: graph[i]){
            if(j == parent[i][0])
                continue;
            parent[j][0] = i;
            depth[j] = depth[i] + 1;
            cost[j] += cost[i];
            que.push(j);
        }
    }
    for(int i=1; i<maxlog; ++i)
        for(int j=0; j<n; ++j)
            parent[j][i] = parent[parent[j][i-1]][i-1];
    int m, res = 0;
    cin >> m;
    for(int i=0; i<m; ++i){
        int a, b, c;
        cin >> a >> b >> c;
        res += (cost[a] + cost[b]) * c;
        if(depth[a] > depth[b])
            swap(a, b);
        for(int i=maxlog-1; i>=0; --i)
            if(depth[parent[b][i]] >= depth[a])
                b = parent[b][i];
        if(a != b){
            for(int i=maxlog-1; i>=0; --i)
                if(parent[a][i] != parent[b][i]){
                    a = parent[a][i];
                    b = parent[b][i];
                }
            a = parent[a][0];
        }
        res -= (cost[a]*2 - u[a]) * c;
    }
    cout << res << endl;
    return 0;
}
0