結果

問題 No.898 tri-βutree
ユーザー pazzle1230pazzle1230
提出日時 2019-10-04 23:47:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 540 ms / 4,000 ms
コード長 2,579 bytes
コンパイル時間 2,259 ms
コンパイル使用メモリ 176,776 KB
実行使用メモリ 37,888 KB
最終ジャッジ日時 2024-04-26 08:46:16
合計ジャッジ時間 12,421 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 352 ms
37,888 KB
testcase_01 AC 4 ms
6,144 KB
testcase_02 AC 4 ms
6,272 KB
testcase_03 AC 3 ms
6,400 KB
testcase_04 AC 4 ms
6,272 KB
testcase_05 AC 4 ms
6,144 KB
testcase_06 AC 4 ms
6,272 KB
testcase_07 AC 523 ms
32,256 KB
testcase_08 AC 528 ms
32,128 KB
testcase_09 AC 518 ms
32,256 KB
testcase_10 AC 517 ms
32,128 KB
testcase_11 AC 509 ms
32,128 KB
testcase_12 AC 522 ms
32,128 KB
testcase_13 AC 519 ms
32,256 KB
testcase_14 AC 523 ms
32,256 KB
testcase_15 AC 516 ms
32,384 KB
testcase_16 AC 516 ms
32,128 KB
testcase_17 AC 540 ms
32,128 KB
testcase_18 AC 511 ms
32,128 KB
testcase_19 AC 512 ms
32,128 KB
testcase_20 AC 533 ms
32,256 KB
testcase_21 AC 533 ms
32,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

int64 N;
vector<PLL> G[112345];

vector<int64> dep, d_sum;
vector<vector<int64>> table;

void dfs(int64 v, int64 p, int64 d = 0, int64 w = 0) {
    dep[v] = d;
    d_sum[v] = d_sum[p] + w;
    table[v][0] = p;
    REP(i, G[v].size()) {
        if (G[v][i].sc == p) continue;
        dfs(G[v][i].sc, v, d+1, G[v][i].fs);
    }
}

void build() {
    dep.resize(N, 0);
    d_sum.resize(N, 0);
    table.resize(N, vector<int64>(20, 0));
    dfs(0, 0);
    FOR(i, 1, 20) {
        REP(j, N) {
            table[j][i] = table[table[j][i-1]][i-1];
        }
    }
}

int64 lca(int64 u, int64 v) {
    if (dep[u] < dep[v]) swap(u, v);
    REP(i, 20) {
        if ((dep[u]-dep[v]) >> i & 1) {
            u = table[u][i];
        }
    }
    if (u == v) return u;
    for (int64 i = 19; i >= 0; i--) {
        if (table[u][i] != table[v][i]) {
            u = table[u][i];
            v = table[v][i];
        }
    }
    return table[u][0];
}

int main(void) {
    cin >> N;
    REP(i, N-1) {
        int64 u, v, w;
        cin >> u >> v >> w;
        G[u].emplace_back(w, v);
        G[v].emplace_back(w, u);
    }
    build();
    int64 Q;
    cin >> Q;
    REP(i, Q) {
        vector<int64> vv(3);
        REP(j, 3) cin >> vv[j];
        int64 res = 0;
        REP(j, 3) {
            FOR(k, j+1, 3) {
                res += d_sum[vv[j]] + d_sum[vv[k]] - 2 * d_sum[lca(vv[j], vv[k])];
            }
        }
        cout << res/2 << endl;
    }
}
0