結果

問題 No.386 貪欲な領主
ユーザー face4face4
提出日時 2019-03-18 13:22:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,494 bytes
コンパイル時間 688 ms
コンパイル使用メモリ 71,076 KB
実行使用メモリ 22,548 KB
最終ジャッジ日時 2023-09-25 09:06:18
合計ジャッジ時間 6,295 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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: 関数 ‘int lca(int, int)’ 内:
main.cpp:32:1: 警告: 制御が非 void 関数の終りに到達しました [-Wreturn-type]
   32 | }
      | ^

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;

vector<int> edge[100000], tax(100000), atax(100000);
int parent[20][100000];
int depth[100000];

void dfs(int v, int p, int d){
    parent[0][v] = p;
    depth[v] = d;
    atax[v] = (p == -1 ? 0 : atax[p]) + tax[v];
    for(int next : edge[v]){
        if(next == p)   continue;
        dfs(next, v, d+1);
    }
}

int lca(int u, int v){
    if(depth[u] > depth[v]) swap(u, v);
    for(int k = 0; k < 20; k++){
        if((depth[v]-depth[u])>>k & 1)  v = parent[k][v];
    }
    if(u == v)  return u;
    for(int k = 19; k >= 0; k--){
        if(parent[k][u] != parent[k][v]){
            u = parent[k][u];
            v = parent[k][v];
        }
    }
}

// なんかバグる
int main(){
    int n;
    cin >> n;
    for(int i = 1; i < n; i++){
        int a, b;   cin >> a >> b;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    for(int i = 0; i < n; i++)  cin >> tax[i];
    
    dfs(0, -1, 0);
    for(int k = 0; k+1 < 20; k++){
        for(int j = 0; j < n; j++){
            if(parent[k][j] < 0)    parent[k+1][j] = -1;
            else                    parent[k+1][j] = parent[k][parent[k][j]];
        }
    }

    int m;
    cin >> m;
    ll ans = 0;
    while(m-- > 0){
        int a, b, c;
        cin >> a >> b >> c;
        int l = lca(a,b);
        ans += (atax[a]+atax[b]-2*(atax[l])+tax[l])*c;
        cout << ans << endl;
    }
    
    cout << ans << endl;

    return 0;
}
0