結果

問題 No.898 tri-βutree
ユーザー yuji9511yuji9511
提出日時 2019-10-04 22:57:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 318 ms / 4,000 ms
コード長 3,484 bytes
コンパイル時間 1,595 ms
コンパイル使用メモリ 166,452 KB
実行使用メモリ 52,224 KB
最終ジャッジ日時 2024-04-26 08:41:22
合計ジャッジ時間 8,445 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 204 ms
52,224 KB
testcase_01 AC 12 ms
44,160 KB
testcase_02 AC 13 ms
43,992 KB
testcase_03 AC 13 ms
44,032 KB
testcase_04 AC 13 ms
44,076 KB
testcase_05 AC 13 ms
44,092 KB
testcase_06 AC 12 ms
44,160 KB
testcase_07 AC 298 ms
49,272 KB
testcase_08 AC 290 ms
49,280 KB
testcase_09 AC 299 ms
49,152 KB
testcase_10 AC 292 ms
49,152 KB
testcase_11 AC 286 ms
49,312 KB
testcase_12 AC 276 ms
49,260 KB
testcase_13 AC 282 ms
49,112 KB
testcase_14 AC 280 ms
49,268 KB
testcase_15 AC 285 ms
49,260 KB
testcase_16 AC 273 ms
49,264 KB
testcase_17 AC 318 ms
49,152 KB
testcase_18 AC 276 ms
49,200 KB
testcase_19 AC 313 ms
49,256 KB
testcase_20 AC 288 ms
49,192 KB
testcase_21 AC 276 ms
49,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> lpair;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}

struct Tree {
    private:
        ll N, logN;
        vector<lpair> edge[200010];
        ll depth[200010] = {};
        ll sum[200010] = {};
        ll parent[200010] = {};
        ll next_val[20][200010] = {};
    public:
        Tree(){
        }

        void init(ll n){
            N = n;
            logN = floor(log2(N)) + 1;
        }

        ll getDepth(ll x) { return depth[x]; }
        ll getSum(ll x) { return sum[x]; }

        void add(ll x, ll y, ll z){ edge[x].push_back({y,z}); }

        void prepare(){
            dfs_parent();
            dfs_sum();
            makeDoubling();
        }

        void dfs_parent(ll cur = 0, ll par = -1, ll d = 0){
            depth[cur] = d;
            parent[cur] = par;
            for(auto &e: edge[cur]){
                if(e.first == par) continue;
                dfs_parent(e.first, cur, d+1);
            }
        }

        void dfs_sum(ll cur = 0, ll par = -1, ll d = 0){
            if(par != -1) sum[cur] = sum[par] + d;
            for(auto &e: edge[cur]){
                if(e.first == par) continue;
                dfs_sum(e.first, cur, e.second);
            }
        }

        void makeDoubling(){
            rep(i,0,N) next_val[0][i] = parent[i];
            rep(k,0,logN){
                rep(i,0,N){
                    if(next_val[k][i] == -1) next_val[k+1][i] = -1;
                    else next_val[k+1][i] = next_val[k][next_val[k][i]];
                }
            }
        }


        ll getParent(ll cur, ll num) {
            ll p = cur;
            rrep(k,logN-1, 0){
                if(p == -1) break;
                if((num >> k) & 1) p = next_val[k][p];
            }
            return p;
        }

        ll getLCA(ll va, ll vb){
            if(depth[va] > depth[vb]){
                va = getParent(va, depth[va] - depth[vb]);
            }else if(depth[va] < depth[vb]){
                vb = getParent(vb, depth[vb] - depth[va]);
            }
            ll lv = -1, rv = N+1;
            while(rv - lv > 1){
                ll mid = (rv + lv) / 2;
                ll ta = getParent(va, mid);
                ll tb = getParent(vb, mid);
                if(ta == -1 || tb == -1){
                    rv = mid;
                }else if(ta != tb){
                    lv = mid;
                }else{
                    rv = mid;
                }
            }
            return getParent(va, rv);
        }
} tree;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    tree.init(N);
    rep(i,0,N-1){
        ll u,v,w;
        cin >> u >> v >> w;
        tree.add(u,v,w);
        tree.add(v,u,w);
    }

    tree.prepare();
    ll Q;
    cin >> Q;
    while(Q--){
        ll x,y,z;
        cin >> x >> y >> z;
        ll ans = tree.getSum(x) + tree.getSum(y) + tree.getSum(z);
        ll v1 = tree.getLCA(x,y);
        ll v2 = tree.getLCA(y,z);
        ll v3 = tree.getLCA(x,z);
        ans -= (tree.getSum(v1) + tree.getSum(v2) + tree.getSum(v3));
        print(ans);
    }
    

}
0