結果

問題 No.1094 木登り / Climbing tree
ユーザー furafura
提出日時 2020-06-28 01:52:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 2,109 bytes
コンパイル時間 2,386 ms
コンパイル使用メモリ 208,416 KB
実行使用メモリ 51,248 KB
最終ジャッジ日時 2024-04-25 18:42:21
合計ジャッジ時間 13,258 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 384 ms
45,112 KB
testcase_02 AC 111 ms
51,248 KB
testcase_03 AC 46 ms
6,940 KB
testcase_04 AC 83 ms
20,740 KB
testcase_05 AC 145 ms
39,504 KB
testcase_06 AC 135 ms
15,456 KB
testcase_07 AC 374 ms
45,104 KB
testcase_08 AC 370 ms
45,104 KB
testcase_09 AC 372 ms
45,108 KB
testcase_10 AC 376 ms
45,024 KB
testcase_11 AC 354 ms
45,108 KB
testcase_12 AC 371 ms
45,108 KB
testcase_13 AC 357 ms
45,108 KB
testcase_14 AC 380 ms
45,108 KB
testcase_15 AC 143 ms
13,312 KB
testcase_16 AC 267 ms
39,812 KB
testcase_17 AC 197 ms
24,596 KB
testcase_18 AC 176 ms
19,000 KB
testcase_19 AC 226 ms
32,920 KB
testcase_20 AC 365 ms
45,372 KB
testcase_21 AC 199 ms
25,776 KB
testcase_22 AC 369 ms
45,364 KB
testcase_23 AC 346 ms
45,164 KB
testcase_24 AC 350 ms
45,140 KB
testcase_25 AC 356 ms
45,116 KB
testcase_26 AC 369 ms
45,236 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>
void add_directed_edge(weighted_graph<T>& G,int u,int v,const T& wt){
	G[u].emplace_back(v,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;
		if(dep[u]>dep[v]) swap(u,v);
		rep(i,h) if((dep[v]-dep[u])>>i&1) res+=dist[i][v], v=par[i][v];
		if(u==v) return res;

		for(int i=h-1;i>=0;i--) if(par[i][u]!=par[i][v]) {
			res+=dist[i][u]; u=par[i][u];
			res+=dist[i][v]; v=par[i][v];
		}
		res+=dist[0][u]+dist[0][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