結果

問題 No.1094 木登り / Climbing tree
ユーザー furafura
提出日時 2020-06-28 02:00:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 456 ms / 2,000 ms
コード長 1,867 bytes
コンパイル時間 2,835 ms
コンパイル使用メモリ 207,568 KB
実行使用メモリ 51,376 KB
最終ジャッジ日時 2024-04-25 18:43:12
合計ジャッジ時間 13,843 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 409 ms
45,240 KB
testcase_02 AC 117 ms
51,376 KB
testcase_03 AC 60 ms
6,940 KB
testcase_04 AC 92 ms
20,864 KB
testcase_05 AC 167 ms
39,500 KB
testcase_06 AC 174 ms
15,456 KB
testcase_07 AC 456 ms
45,104 KB
testcase_08 AC 450 ms
45,232 KB
testcase_09 AC 442 ms
45,108 KB
testcase_10 AC 427 ms
45,108 KB
testcase_11 AC 422 ms
45,364 KB
testcase_12 AC 401 ms
45,360 KB
testcase_13 AC 407 ms
45,236 KB
testcase_14 AC 420 ms
45,112 KB
testcase_15 AC 172 ms
13,184 KB
testcase_16 AC 320 ms
39,936 KB
testcase_17 AC 223 ms
24,344 KB
testcase_18 AC 199 ms
19,260 KB
testcase_19 AC 272 ms
33,052 KB
testcase_20 AC 446 ms
45,108 KB
testcase_21 AC 238 ms
25,908 KB
testcase_22 AC 416 ms
45,236 KB
testcase_23 AC 416 ms
45,108 KB
testcase_24 AC 431 ms
45,240 KB
testcase_25 AC 402 ms
45,236 KB
testcase_26 AC 424 ms
45,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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

using namespace std;

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;
	vector<vector<T>> dist;
	const weighted_graph<T>& Tr;

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

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));
		dist.assign(h,vector<T>(n));

		dfs(root,-1);
		rep(i,h-1) rep(u,n) if(par[i][u]!=-1) {
			par[i+1][u]=par[i][par[i][u]];
			dist[i+1][u]=dist[i][u]+dist[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];
	}

	T distance(int u,int v)const{
		int h=par.size();
		T res=0;
		int w=lca(u,v);
		rep(i,h){
			if((dep[u]-dep[w])>>i&1) res+=dist[i][u], u=par[i][u];
			if((dep[v]-dep[w])>>i&1) res+=dist[i][v], v=par[i][v];
		}
		return res;
	}
};

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

	lowest_common_ancestor<int> LCA(T,0);

	int q; scanf("%d",&q);
	rep(_,q){
		int s,t; scanf("%d%d",&s,&t); s--; t--;
		printf("%d\n",LCA.distance(s,t));
	}

	return 0;
}
0