結果

問題 No.898 tri-βutree
ユーザー recososorecososo
提出日時 2020-07-10 21:03:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 379 ms / 4,000 ms
コード長 2,596 bytes
コンパイル時間 2,511 ms
コンパイル使用メモリ 183,940 KB
実行使用メモリ 26,180 KB
最終ジャッジ日時 2023-08-08 16:51:09
合計ジャッジ時間 10,332 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 221 ms
26,180 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,388 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,388 KB
testcase_07 AC 353 ms
24,440 KB
testcase_08 AC 370 ms
24,416 KB
testcase_09 AC 361 ms
24,404 KB
testcase_10 AC 375 ms
24,360 KB
testcase_11 AC 351 ms
24,392 KB
testcase_12 AC 365 ms
24,456 KB
testcase_13 AC 360 ms
24,532 KB
testcase_14 AC 359 ms
24,416 KB
testcase_15 AC 358 ms
24,408 KB
testcase_16 AC 379 ms
24,444 KB
testcase_17 AC 372 ms
24,448 KB
testcase_18 AC 369 ms
24,364 KB
testcase_19 AC 373 ms
24,352 KB
testcase_20 AC 373 ms
24,436 KB
testcase_21 AC 377 ms
24,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define FORR(i, m, n) for (ll i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
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 (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
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];
  }
};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;cin >> n;
    vector<vector<pair<ll,ll>>> e(n);
    vector<vector<int>> g(n);
    REP(i,n-1){
        int u,v,w;cin >> u >> v >> w;
        e[u].push_back({v,w});
        e[v].push_back({u,w});
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<ll> d(n,-1);
    d[0]=0;
    stack<int> st;
    st.push(0);
    while(!st.empty()){
        int p=st.top();
        st.pop();
        for(auto x:e[p]){
            if(d[x.first]==-1){
                d[x.first]=d[p]+x.second;
                st.push(x.first);
            }
        }
    }
    DoublingLowestCommonAncestor<vector<vector<int>>> lca(g);
    lca.build();
    int q;cin >> q;
    while(q--){
        int x,y,z;cin >> x >> y >> z;
        cout << d[x]+d[y]+d[z]-d[lca.query(x,y)]-d[lca.query(y,z)]-d[lca.query(z,x)] << endl;
    }
}
0