結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:6:13: warning: overflow in conversion from ‘long int’ to ‘int’ changes value from ‘1624367077768’ to ‘869439880’ [-Woverflow]
     int x = 1624367077768;
             ^~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
    int x = 1624367077768;
    cout << x  << endl;
    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;
    cin >> m;
    long long res = 0;
    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