結果

問題 No.898 tri-βutree
ユーザー kotatsugamekotatsugame
提出日時 2019-10-12 02:22:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 449 ms / 4,000 ms
コード長 1,132 bytes
コンパイル時間 1,059 ms
コンパイル使用メモリ 72,712 KB
実行使用メモリ 25,784 KB
最終ジャッジ日時 2023-08-08 16:26:21
合計ジャッジ時間 9,975 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 291 ms
25,784 KB
testcase_01 AC 4 ms
14,664 KB
testcase_02 AC 5 ms
14,672 KB
testcase_03 AC 4 ms
14,576 KB
testcase_04 AC 5 ms
14,668 KB
testcase_05 AC 4 ms
14,808 KB
testcase_06 AC 4 ms
14,660 KB
testcase_07 AC 429 ms
19,944 KB
testcase_08 AC 433 ms
19,928 KB
testcase_09 AC 429 ms
20,088 KB
testcase_10 AC 427 ms
19,928 KB
testcase_11 AC 429 ms
20,004 KB
testcase_12 AC 449 ms
20,188 KB
testcase_13 AC 424 ms
19,928 KB
testcase_14 AC 426 ms
19,960 KB
testcase_15 AC 439 ms
20,008 KB
testcase_16 AC 430 ms
19,940 KB
testcase_17 AC 436 ms
19,932 KB
testcase_18 AC 432 ms
19,944 KB
testcase_19 AC 434 ms
19,948 KB
testcase_20 AC 428 ms
20,116 KB
testcase_21 AC 430 ms
20,004 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:38:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
   38 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
int N;
vector<pair<int,int> >G[1<<17];
int depth[1<<17];
long weighth[1<<17];
int parent[17][1<<17];
void dfs(int u,int p,int d,long wei)
{
	depth[u]=d;
	weighth[u]=wei;
	parent[0][u]=p;
	for(pair<int,int>q:G[u])
	{
		if(q.first==p)continue;
		dfs(q.first,u,d+1,wei+q.second);
	}
}
int lca(int u,int v)
{
	if(depth[u]>depth[v])swap(u,v);
	for(int k=16;k>=0;k--)
	{
		if(depth[v]-depth[u]>>k&1)v=parent[k][v];
	}
	if(u==v)return u;
	for(int k=16;k>=0;k--)
	{
		if(parent[k][u]!=parent[k][v])
		{
			u=parent[k][u];
			v=parent[k][v];
		}
	}
	return parent[0][u];
}
main()
{
	cin>>N;
	for(int i=1;i<N;i++)
	{
		int u,v,w;cin>>u>>v>>w;
		G[u].push_back(make_pair(v,w));
		G[v].push_back(make_pair(u,w));
	}
	dfs(0,-1,0,0);
	for(int k=1;k<17;k++)
	{
		for(int i=0;i<N;i++)
		{
			if(parent[k-1][i]>=0)parent[k][i]=parent[k-1][parent[k-1][i]];
			else parent[k][i]=-1;
		}
	}
	int Q;
	cin>>Q;
	for(;Q--;)
	{
		int x,y,z;cin>>x>>y>>z;
		int xy=lca(x,y);
		int yz=lca(y,z);
		int zx=lca(z,x);
		cout<<weighth[x]+weighth[y]+weighth[z]-weighth[xy]-weighth[yz]-weighth[zx]<<endl;
	}
}
0