結果

問題 No.898 tri-βutree
ユーザー erbowlerbowl
提出日時 2020-04-25 16:46:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 487 ms / 4,000 ms
コード長 3,813 bytes
コンパイル時間 2,565 ms
コンパイル使用メモリ 215,552 KB
実行使用メモリ 31,412 KB
最終ジャッジ日時 2023-08-08 16:45:54
合計ジャッジ時間 12,195 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 317 ms
31,412 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 473 ms
24,104 KB
testcase_08 AC 471 ms
24,104 KB
testcase_09 AC 473 ms
23,944 KB
testcase_10 AC 475 ms
24,040 KB
testcase_11 AC 474 ms
23,956 KB
testcase_12 AC 469 ms
23,916 KB
testcase_13 AC 468 ms
23,908 KB
testcase_14 AC 483 ms
23,864 KB
testcase_15 AC 468 ms
23,940 KB
testcase_16 AC 471 ms
24,052 KB
testcase_17 AC 468 ms
24,040 KB
testcase_18 AC 471 ms
23,872 KB
testcase_19 AC 485 ms
24,092 KB
testcase_20 AC 487 ms
24,100 KB
testcase_21 AC 471 ms
24,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
template<class Monoid> struct SegTree {
    using Func = function<Monoid(Monoid, Monoid)>;
    const Func F;
    const Monoid UNITY;
    int SIZE_R;
    vector<Monoid> dat;
    
    SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) { init(n); }
    void init(int n) {
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        dat.assign(SIZE_R * 2, UNITY);
    }
    
    /* set, a is 0-indexed */
    void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
    void build() {
        for (int k = SIZE_R - 1; k > 0; --k)
            dat[k] = F(dat[k*2], dat[k*2+1]);
    }
    
    /* update a, a is 0-indexed */
    void update(int a, const Monoid &v) {
        int k = a + SIZE_R;
        dat[k] = v;
        while (k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]);
    }
    
    /* get [a, b), a and b are 0-indexed */
    Monoid get(int a, int b) {
        Monoid vleft = UNITY, vright = UNITY;
        for (int left = a + SIZE_R, right = b + SIZE_R; left < right; left >>= 1, right >>= 1) {
            if (left & 1) vleft = F(vleft, dat[left++]);
            if (right & 1) vright = F(dat[--right], vright);
        }                                                                                                              
        return F(vleft, vright);
    }
    inline Monoid operator [] (int a) { return dat[a + SIZE_R]; }
    
    /* debug */
    void print() {
        for (int i = 0; i < SIZE_R; ++i) {
            cout << (*this)[i];
            if (i != SIZE_R-1) cout << ",";
        }
        cout << endl;
    }
};



using Graph = vector<vector<int> >;
struct LCA {
    vector<vector<int> > parent; // parent[d][v] := 2^d-th parent of v
    vector<int> depth;
    LCA() { }
    LCA(const Graph &G, int r = 0) { init(G, r); }
    void init(const Graph &G, int r = 0) {
        int V = (int)G.size();
        int h = 1;
        while ((1<<h) < V) ++h;
        parent.assign(h, vector<int>(V, -1));
        depth.assign(V, -1);
        dfs(G, r, -1, 0);
        for (int i = 0; i+1 < (int)parent.size(); ++i)
            for (int v = 0; v < V; ++v)
                if (parent[i][v] != -1)
                    parent[i+1][v] = parent[i][parent[i][v]];
    }
    void dfs(const Graph &G, int v, int p, int d) {
        parent[0][v] = p;
        depth[v] = d;
        for (auto e : G[v]) if (e != p) dfs(G, e, v, d+1);
    }
    int get(int u, int v) {
        if (depth[u] > depth[v]) swap(u, v);
        for (int i = 0; i < (int)parent.size(); ++i)
            if ( (depth[v] - depth[u]) & (1<<i) )
                v = parent[i][v];
        if (u == v) return u;
        for (int i = (int)parent.size()-1; i >= 0; --i) {
            if (parent[i][u] != parent[i][v]) {
                u = parent[i][u];
                v = parent[i][v];
            }
        }
        return parent[0][u];
    }
};



int main() {
    ll n;
    std::cin >> n;
    vector<vector<pair<ll,ll>>> edges(n);
    
    Graph G(n);

    for (int i = 0; i < n-1; i++) {
        ll a,b,c;
        std::cin >> a>>b>>c;
        edges[b].push_back({a,c});  
        edges[a].push_back({b,c});
        G[a].push_back(b);
        G[b].push_back(a);
        
    }
    
    LCA lca(G);

    vector<ll> dist(n,0);
    function<void(int, int)> rec = [&] (int par, int cur) {
        for (auto e : edges[cur]) {
            if(e.first==par)continue;
            dist[e.first] = dist[cur]+e.second;
            rec(cur, e.first);
        }
    };
     
    rec(-1,0);
    
    ll q;
    std::cin >> q;
    
    for (int i = 0; i < q; i++) {
        ll x,y,z;
        std::cin >> x>>y>>z;
        
        cout<<dist[x]+dist[y]+dist[z]-(dist[lca.get(x,y)]+dist[lca.get(z,y)]+dist[lca.get(x,z)])
    
        <<'\n';
    }
    
    
}

0