結果

問題 No.898 tri-βutree
ユーザー ugis_progugis_prog
提出日時 2019-10-04 23:55:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 516 ms / 4,000 ms
コード長 2,259 bytes
コンパイル時間 2,119 ms
コンパイル使用メモリ 181,880 KB
実行使用メモリ 30,036 KB
最終ジャッジ日時 2023-08-08 16:09:21
合計ジャッジ時間 12,686 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 303 ms
30,036 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 508 ms
24,676 KB
testcase_08 AC 496 ms
24,428 KB
testcase_09 AC 501 ms
24,484 KB
testcase_10 AC 500 ms
24,488 KB
testcase_11 AC 506 ms
24,552 KB
testcase_12 AC 512 ms
24,420 KB
testcase_13 AC 511 ms
24,544 KB
testcase_14 AC 516 ms
24,424 KB
testcase_15 AC 504 ms
24,412 KB
testcase_16 AC 512 ms
24,376 KB
testcase_17 AC 494 ms
24,484 KB
testcase_18 AC 506 ms
24,500 KB
testcase_19 AC 499 ms
24,372 KB
testcase_20 AC 504 ms
24,464 KB
testcase_21 AC 509 ms
24,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

template< typename G >
struct DoublingLowestCommonAncestor {
  const int LOG;
  vector< int > dep;
  const G &g;
  vector< vector< int > > table;

  DoublingLowestCommonAncestor(const G &g) : g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
    table.assign(LOG, vector< int >(g.size(), -1));
  }

  void dfs(int idx, int par, int d) {
    table[0][idx] = par;
    dep[idx] = d;
    for(auto &to : g[idx]) {
      if(to != par) dfs(to, idx, d + 1);
    }
  }

  void build() {
    dfs(0, -1, 0);
    for(int k = 0; k + 1 < LOG; k++) {
      for(int i = 0; i < table[k].size(); i++) {
        if(table[k][i] == -1) table[k + 1][i] = -1;
        else table[k + 1][i] = table[k][table[k][i]];
      }
    }
  }

  int query(int u, int v) {
    if(dep[u] > dep[v]) swap(u, v);
    for(int i = LOG - 1; i >= 0; i--) {
      if(((dep[v] - dep[u]) >> i) & 1) v = table[i][v];
    }
    if(u == v) return u;
    for(int i = LOG - 1; i >= 0; i--) {
      if(table[i][u] != table[i][v]) {
        u = table[i][u];
        v = table[i][v];
      }
    }
    
    return table[0][u];
  }
};

vector<long long> wei;
void dfs(vector<vector<pair<int,long long>>>& g, int v, int p){
    for(auto e : g[v]){
        if(e.first == p) continue;
        wei[e.first] += (e.second + wei[v]);
        dfs(g, e.first, v);
    }
}

int main(){
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    vector<vector<pair<int,long long>>> g(N);
    wei.assign(N, 0);
    for(int i = 0; i < N-1; i++){
        int u,v,w;
        cin >> u >> v >> w;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
        g[u].emplace_back(make_pair(v,w));
        g[v].emplace_back(make_pair(u,w));
    }
    
    dfs(g, 0, -1);
    DoublingLowestCommonAncestor<vector<vector<int>>> lca(G);
    lca.build();

    int Q, x, y, z;
    cin >> Q;
    for(int i = 0; i < Q; i++){
        cin >> x >> y >> z;
        int a = lca.query(x, y);
        int b = lca.query(x, z);
        int c = lca.query(y, z);
        long long W = 0;
        W += (wei[x] - wei[a]) + (wei[y] - wei[a]);
        W += (wei[x] - wei[b]) + (wei[z] - wei[b]);
        W += (wei[y] - wei[c]) + (wei[z] - wei[c]);
        W /= 2;
        cout << W << endl;
    }
}
0