結果

問題 No.898 tri-βutree
ユーザー season1618season1618
提出日時 2019-10-04 23:08:09
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,108 bytes
コンパイル時間 1,558 ms
コンパイル使用メモリ 176,972 KB
実行使用メモリ 51,424 KB
最終ジャッジ日時 2024-04-14 11:34:45
合計ジャッジ時間 12,830 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define range(a) a.begin(),a.end()
#define print(A,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i];}cout<<endl;
#define pprint(A,m,n) rep(j,0,m){print(A[j],n);}
const long mod=1e9+7;
const int size=1e5;
const int INF=1e9;
vector<int> g[size];
map<int,map<int,long>> len;
int par[size];int depth[size]={};
void dfs(int p,int v){
	for(int u:g[v]){
		if(u==p) continue;
        par[u]=v;depth[u]=depth[v]+1;
		dfs(v,u);
	}return;
}
int main(){
	int N;cin>>N;
	int a,b,w;
	rep(i,0,N-1){
		cin>>a>>b>>w;
		g[a].push_back(b);
		g[b].push_back(a);
		len[a][b]=w;
		len[b][a]=w;
	}
	par[0]=-1;dfs(-1,0);
	int Q;cin>>Q;
	vector<pair<int,int>> x(3);//print(par,N);
    long sum;
	rep(i,0,Q){
		cin>>x[0].second>>x[1].second>>x[2].second;
        rep(i,0,3) x[i].first=-depth[x[i].second];
        sort(x.begin(),x.end());
        int count[N]={};
		sum=0;
		rep(j,0,3){
			int v=x[j].second;
			while(v!=0){
                if(count[v]>0) break;
                count[v]++;
				sum+=len[v][par[v]];
				v=par[v];
			}
		}cout<<sum<<endl;
	}
}
0