結果
| 問題 |
No.898 tri-βutree
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-10-12 02:22:26 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 489 ms / 4,000 ms |
| コード長 | 1,132 bytes |
| コンパイル時間 | 1,007 ms |
| コンパイル使用メモリ | 71,680 KB |
| 実行使用メモリ | 23,936 KB |
| 最終ジャッジ日時 | 2024-11-08 23:06:11 |
| 合計ジャッジ時間 | 10,856 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 21 |
コンパイルメッセージ
main.cpp:38:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
38 | main()
| ^~~~
ソースコード
#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;
}
}