結果
| 問題 | 
                            No.1094 木登り / Climbing tree
                             | 
                    
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-06-24 07:40:41 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 922 bytes | 
| コンパイル時間 | 717 ms | 
| コンパイル使用メモリ | 72,908 KB | 
| 実行使用メモリ | 44,800 KB | 
| 最終ジャッジ日時 | 2024-09-14 22:29:49 | 
| 合計ジャッジ時間 | 20,805 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 7 WA * 19 | 
コンパイルメッセージ
main.cpp:33:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   33 | main()
      | ^~~~
            
            ソースコード
#include<iostream>
#include<vector>
using namespace std;
int N;
vector<pair<int,int> >G[2<<17];
int dep[2<<17],pr[18][2<<17];
long cost[2<<17];
void dfs(int u,int p,int d,int c)
{
	dep[u]=d;
	cost[u]=c;
	pr[0][u]=p;
	for(pair<int,int>e:G[u])if(e.first!=p)
	{
		dfs(e.first,u,d+1,c+e.second);
	}
}
int lca(int u,int v)
{
	if(dep[u]<dep[v])u^=v^=u^=v;
	for(int k=0;k<18;k++)if(dep[u]-dep[v]>>k&1)
	{
		u=pr[k][u];
	}
	if(u==v)return u;
	for(int k=18;k--;)if(pr[k][u]!=pr[k][v])
	{
		k=pr[k][u];
		v=pr[k][v];
	}
	return pr[0][u];
}
main()
{
	cin>>N;
	for(int i=1;i<N;i++)
	{
		int a,b,c;cin>>a>>b>>c;
		a--,b--;
		G[a].push_back(make_pair(b,c));
		G[b].push_back(make_pair(a,c));
	}
	dfs(0,-1,0,0);
	for(int k=1;k<18;k++)for(int i=0;i<N;i++)
	{
		pr[k][i]=pr[k-1][i]==-1?-1:pr[k-1][pr[k-1][i]];
	}
	int Q;cin>>Q;
	for(;Q--;)
	{
		int u,v;cin>>u>>v;
		u--,v--;
		int w=lca(u,v);
		cout<<cost[u]+cost[v]-cost[w]*2<<endl;
	}
}