結果

問題 No.898 tri-βutree
ユーザー yuji9511yuji9511
提出日時 2020-01-21 01:35:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 476 ms / 4,000 ms
コード長 2,296 bytes
コンパイル時間 2,100 ms
コンパイル使用メモリ 171,788 KB
実行使用メモリ 31,872 KB
最終ジャッジ日時 2024-04-26 09:12:18
合計ジャッジ時間 10,148 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 180 ms
31,872 KB
testcase_01 AC 4 ms
6,144 KB
testcase_02 AC 4 ms
6,016 KB
testcase_03 AC 3 ms
6,144 KB
testcase_04 AC 3 ms
5,888 KB
testcase_05 AC 4 ms
6,016 KB
testcase_06 AC 3 ms
6,144 KB
testcase_07 AC 332 ms
27,520 KB
testcase_08 AC 340 ms
27,392 KB
testcase_09 AC 347 ms
27,648 KB
testcase_10 AC 346 ms
27,648 KB
testcase_11 AC 380 ms
27,648 KB
testcase_12 AC 341 ms
27,648 KB
testcase_13 AC 363 ms
27,520 KB
testcase_14 AC 364 ms
27,648 KB
testcase_15 AC 367 ms
27,392 KB
testcase_16 AC 350 ms
27,520 KB
testcase_17 AC 357 ms
27,648 KB
testcase_18 AC 476 ms
27,648 KB
testcase_19 AC 369 ms
27,392 KB
testcase_20 AC 361 ms
27,392 KB
testcase_21 AC 361 ms
27,520 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)...);}
vector<lpair> tree[100010];
ll parent[100010] = {};
ll depth[100010] = {};
ll next_val[25][100010] = {};
ll logN = 0;
ll sum[100010] = {};
ll N;
void dfs2(ll cur, ll par, ll d){
	if(par != -1) sum[cur] = sum[par] + d;
	for(auto &e: tree[cur]){
		if(e.first == par) continue;
		dfs2(e.first, cur, e.second);
	}
}

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


ll get_parent(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 get_LCA(ll va, ll vb){
	if(depth[va] > depth[vb]){
		va = get_parent(va, depth[va] - depth[vb]);
	}else if(depth[va] < depth[vb]){
		vb = get_parent(vb, depth[vb] - depth[va]);
	}
	ll lv = -1, rv = N+1;
	while(rv - lv > 1){
		ll mid = (rv + lv) / 2;
		ll ta = get_parent(va, mid);
		ll tb = get_parent(vb, mid);
		if(ta == -1 || tb == -1){
			rv = mid;
		}else if(ta != tb){
			lv = mid;
		}else{
			rv = mid;
		}
	}
	return get_parent(va, rv);
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> N;
	rep(i,0,N-1){
		ll u,v,w;
		cin >> u >> v >> w;
		tree[u].push_back({v,w});
		tree[v].push_back({u,w});
	}
	logN = floor(log2(N)) + 1;
	dfs(0, -1, 0);
    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]];
            }
        }
    }
	dfs2(0,-1,0);
	ll Q;
	cin >> Q;
	while(Q--){
		ll x,y,z;
		cin >> x >> y >> z;
		ll ans = sum[x] + sum[y] + sum[z] - sum[get_LCA(x,y)] - sum[get_LCA(y,z)] - sum[get_LCA(x,z)];
		print(ans);
	}
	

}
0