結果

問題 No.898 tri-βutree
ユーザー furafura
提出日時 2020-06-01 01:44:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 396 ms / 4,000 ms
コード長 2,674 bytes
コンパイル時間 5,154 ms
コンパイル使用メモリ 229,900 KB
実行使用メモリ 34,400 KB
最終ジャッジ日時 2023-08-08 16:49:50
合計ジャッジ時間 11,929 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
34,400 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 377 ms
31,944 KB
testcase_08 AC 383 ms
31,684 KB
testcase_09 AC 382 ms
31,820 KB
testcase_10 AC 396 ms
31,820 KB
testcase_11 AC 387 ms
31,848 KB
testcase_12 AC 388 ms
31,676 KB
testcase_13 AC 378 ms
31,792 KB
testcase_14 AC 392 ms
31,868 KB
testcase_15 AC 381 ms
31,816 KB
testcase_16 AC 385 ms
31,772 KB
testcase_17 AC 395 ms
31,832 KB
testcase_18 AC 387 ms
31,728 KB
testcase_19 AC 384 ms
31,696 KB
testcase_20 AC 389 ms
31,792 KB
testcase_21 AC 392 ms
31,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

template<class T> struct edge{
	int to;
	T wt;
	edge(int to,const T& wt):to(to),wt(wt){}
};
template<class T> using weighted_graph=vector<vector<edge<T>>>;

template<class T>
void add_undirected_edge(weighted_graph<T>& G,int u,int v,const T& wt){
	G[u].emplace_back(v,wt);
	G[v].emplace_back(u,wt);
}

template<class T>
class lowest_common_ancestor{
	vector<int> dep;
	vector<vector<int>> par;
	const weighted_graph<T>& Tr;

	vector<vector<lint>> wt;

	void dfs(int u,int p,int d){
		dep[u]=d;
		par[0][u]=p;
		for(const auto& e:Tr[u]){
			if(e.to!=p) dfs(e.to,u,d+1);
			else wt[0][u]=e.wt;
		}
	}

public:
	lowest_common_ancestor(const weighted_graph<T>& Tr,int root):Tr(Tr){
		int n=Tr.size(),h;
		for(h=1;(1<<h)<n;h++);

		dep.assign(n,0);
		par.assign(h,vector<int>(n,-1));
		wt.assign(h,vector<lint>(n));

		dfs(root,-1,0);
		rep(i,h-1) rep(u,n) if(par[i][u]!=-1) {
			par[i+1][u]=par[i][par[i][u]];
			wt[i+1][u]=wt[i][u]+wt[i][par[i][u]];
		}
	}

	int lca(int u,int v)const{
		int h=par.size();

		if(dep[u]>dep[v]) swap(u,v);
		rep(i,h) if((dep[v]-dep[u])>>i&1) v=par[i][v];
		if(u==v) return u;

		for(int i=h-1;i>=0;i--) if(par[i][u]!=par[i][v]) u=par[i][u], v=par[i][v];
		return par[0][u];
	}

	int dist(int u,int v)const{ return dep[u]+dep[v]-2*dep[lca(u,v)]; }

	lint solve(int u,int v)const{
		lint res=0;
		int w=lca(u,v);
		for(int i=int(wt.size())-1;i>=0;i--) if(dep[u]-(1<<i)>=dep[w]) {
			res+=wt[i][u];
			u=par[i][u];
		}
		for(int i=int(wt.size())-1;i>=0;i--) if(dep[v]-(1<<i)>=dep[w]) {
			res+=wt[i][v];
			v=par[i][v];
		}
		return res;
	}
};

int main(){
	int n; scanf("%d",&n);
	weighted_graph<lint> T(n);
	rep(i,n-1){
		int u,v;
		lint c; scanf("%d%d%lld",&u,&v,&c);
		add_undirected_edge(T,u,v,c);
	}

	lowest_common_ancestor<lint> LCA(T,0);

	int q; scanf("%d",&q);
	rep(_,q){
		int u[3];
		rep(i,3) scanf("%d",&u[i]);

		sort(u,u+3,[&](int v,int w){
			return LCA.dist(0,v)<LCA.dist(0,w);
		});

		if(LCA.dist(u[0],u[1])+LCA.dist(u[1],u[2])==LCA.dist(u[0],u[2])){ // line
			printf("%lld\n",LCA.solve(u[0],u[2]));
		}
		else{ // Y
			int u01=LCA.lca(u[0],u[1]);
			int u02=LCA.lca(u[0],u[2]);
			int u12=LCA.lca(u[1],u[2]);
			int r=LCA.lca(u01,u02);

			lint ans=0;
			if(u01!=r){
				ans+=LCA.solve(u[0],u[1]);
				ans+=LCA.solve(u01,r);
				ans+=LCA.solve(u[2],r);
			}
			else if(u02!=r){
				ans+=LCA.solve(u[0],u[2]);
				ans+=LCA.solve(u02,r);
				ans+=LCA.solve(u[1],r);
			}
			else{
				ans+=LCA.solve(u[1],u[2]);
				ans+=LCA.solve(u12,r);
				ans+=LCA.solve(u[0],r);
			}
			printf("%lld\n",ans);
		}
	}

	return 0;
}
0