結果

問題 No.1094 木登り / Climbing tree
ユーザー fumofumofunifumofumofuni
提出日時 2021-02-03 00:00:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 905 ms / 2,000 ms
コード長 4,600 bytes
コンパイル時間 2,449 ms
コンパイル使用メモリ 209,692 KB
実行使用メモリ 46,324 KB
最終ジャッジ日時 2024-04-25 19:35:30
合計ジャッジ時間 22,810 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 801 ms
34,568 KB
testcase_02 AC 235 ms
46,324 KB
testcase_03 AC 227 ms
6,944 KB
testcase_04 AC 213 ms
17,408 KB
testcase_05 AC 318 ms
30,600 KB
testcase_06 AC 449 ms
13,696 KB
testcase_07 AC 874 ms
34,684 KB
testcase_08 AC 823 ms
34,592 KB
testcase_09 AC 834 ms
34,564 KB
testcase_10 AC 821 ms
34,564 KB
testcase_11 AC 905 ms
34,564 KB
testcase_12 AC 849 ms
34,560 KB
testcase_13 AC 851 ms
34,704 KB
testcase_14 AC 852 ms
34,560 KB
testcase_15 AC 481 ms
13,568 KB
testcase_16 AC 680 ms
36,244 KB
testcase_17 AC 525 ms
23,384 KB
testcase_18 AC 496 ms
18,536 KB
testcase_19 AC 629 ms
30,720 KB
testcase_20 AC 859 ms
34,704 KB
testcase_21 AC 577 ms
24,636 KB
testcase_22 AC 849 ms
34,480 KB
testcase_23 AC 873 ms
34,688 KB
testcase_24 AC 792 ms
34,560 KB
testcase_25 AC 785 ms
34,696 KB
testcase_26 AC 786 ms
34,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
//const ll dy[8]={-1,0,1,0,1,1,-1,-1};
//const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}


struct Edge {
    long long to;
    long long w;
};
using Graph = vector<vector<Edge>>;
/* LCA(G, root): 木 G に対する根を root として Lowest Common Ancestor を求める構造体
    query(u,v): u と v の LCA を求める。計算量 O(logn)
    前処理: O(nlogn)時間, O(nlogn)空間
*/
struct LCA {
    vector<vector<int>> parent;  // parent[k][u]:= u の 2^k 先の親
    vector<int> dist;            // root からの距離
    //vector<vector<ll>> sumcost;
    LCA(const Graph &G, int root = 0) { init(G, root); }
    // 初期化
    void init(const Graph &G, int root = 0) {
        int V = G.size();
        int K = 1;
        while ((1 << K) < V) K++;
        parent.assign(K, vector<int>(V, -1));
        dist.assign(V, -1);
        //sumcost.assign(K, vector<ll>(V, 0));
        dfs(G, root, -1, 0);
        for (int k = 0; k + 1 < K; k++) {
            for (int v = 0; v < V; v++) {
                if (parent[k][v] >= 0) {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                    //sumcost[k + 1][v] = sumcost[k][v]+sumcost[k][parent[k][v]];
                }
            }
        }
    }
    // 根からの距離と1つ先の頂点を求める
    void dfs(const Graph &G, int v, int p, int d) {
        parent[0][v] = p;
        dist[v] = d;
        for (auto e : G[v]) {
            if (e.to != p) {
                //sumcost[0][e.to]=e.w;
                dfs(G, e.to, v, d + 1);
            }
        }
    }
    int query(int u, int v) {
        if (dist[u] < dist[v]) swap(u, v);  // u の方が深いとする
        int K = parent.size();
        // LCA までの距離を同じにする
        for (int k = 0; k < K; k++) {
            if ((dist[u] - dist[v])&(1<<k) ){
                u = parent[k][u];
            }
        }
        // 二分探索で LCA を求める
        if (u == v) return u;
        for (int k = K - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    /*ll getsumcost(int u, int v) {
        if (dist[u] < dist[v]) swap(u, v);  // u の方が深いとする
        int K = parent.size();
        ll ret=0;
        // LCA までの距離を同じにする
        for (int k = 0; k < K; k++) {
            if ((dist[u] - dist[v])  &  (1<<k) ) {
                ret=ret+sumcost[k][u];
                u = parent[k][u];
            }
        }
        // 二分探索で LCA を求める
        if (u == v) return ret;
        for (int k = K - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                ret=ret+sumcost[k][u]+sumcost[k][v];
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        ret=ret+sumcost[0][u]+sumcost[0][v];
        return ret;
    }*/
    int length(int u, int v) { return dist[u] + dist[v] - 2 * dist[query(u, v)]; }
    bool is_in(int u, int v, int a) { return length(u, a) + length(a, v) == length(u, v); }
};
vl dp(200010);
void dfs(vector<vector<Edge>> &g,ll v,ll par=-1){
    for(auto p:g[v]){
        if(p.to==par)continue;
        dp[p.to]=dp[v]+p.w;
        dfs(g,p.to,v);
    }
}
int main(){
    ll n;cin >> n;
    vector<vector<Edge>> g(n);
    rep(i,n-1){
        ll a,b,c;cin >> a >> b >> c;a--;b--;
        g[a].pb({b,c});
        g[b].pb({a,c});
    }
    LCA lc(g);
    dfs(g,0);
    ll q;cin >> q;
    while(q--){
        ll s,t;cin >> s >> t;s--;t--;
        ll p=lc.query(s,t);
        cout << dp[s]+dp[t]-dp[p]*2 <<endl;
    }

}
0