結果

問題 No.898 tri-βutree
ユーザー tekihei2317tekihei2317
提出日時 2019-10-04 22:03:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 432 ms / 4,000 ms
コード長 2,254 bytes
コンパイル時間 2,147 ms
コンパイル使用メモリ 179,416 KB
実行使用メモリ 38,296 KB
最終ジャッジ日時 2023-08-08 15:59:27
合計ジャッジ時間 11,126 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 236 ms
38,296 KB
testcase_01 AC 3 ms
5,728 KB
testcase_02 AC 4 ms
5,808 KB
testcase_03 AC 4 ms
5,756 KB
testcase_04 AC 3 ms
5,764 KB
testcase_05 AC 3 ms
5,808 KB
testcase_06 AC 3 ms
5,824 KB
testcase_07 AC 425 ms
31,768 KB
testcase_08 AC 426 ms
31,512 KB
testcase_09 AC 432 ms
31,624 KB
testcase_10 AC 413 ms
31,796 KB
testcase_11 AC 424 ms
31,572 KB
testcase_12 AC 418 ms
31,580 KB
testcase_13 AC 425 ms
31,624 KB
testcase_14 AC 419 ms
31,532 KB
testcase_15 AC 418 ms
31,628 KB
testcase_16 AC 418 ms
31,516 KB
testcase_17 AC 416 ms
31,552 KB
testcase_18 AC 420 ms
31,628 KB
testcase_19 AC 412 ms
31,508 KB
testcase_20 AC 429 ms
31,612 KB
testcase_21 AC 420 ms
31,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define int long long
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

struct edge{int to,cost;};
vector<edge> G[100010];

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];
  }
};
typedef vector<vector<int>> vvint;
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N; cin>>N;
    vector<vector<int>> g(N);
    for(int i=0;i<N-1;i++){
        int a,b,c; cin>>a>>b>>c;
        G[a].push_back({b,c});
        G[b].push_back({a,c});
        g[a].push_back(b);
        g[b].push_back(a);
    }
    DoublingLowestCommonAncestor<vvint> lca(g);
    lca.build();

    vector<int> depth(N);
    function<void(int,int)> dfs=[&](int v,int p){
        for(edge e:G[v]) if(e.to!=p){
            depth[e.to]=depth[v]+e.cost;
            dfs(e.to,v);
        }
    };
    dfs(0,-1);

    int Q; cin>>Q;
    while(Q--){
        int x,y,z; cin>>x>>y>>z;
        cout<<depth[x]+depth[y]+depth[z]-depth[lca.query(x,y)]-depth[lca.query(x,z)]-depth[lca.query(y,z)]<<endl;
    }
    // for(int i=0;i<N;i++) cout<<depth[i]<<' '; cout<<endl;
    return 0;
}
0