結果

問題 No.898 tri-βutree
ユーザー 👑 kmjpkmjp
提出日時 2019-10-04 23:27:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 534 ms / 4,000 ms
コード長 1,933 bytes
コンパイル時間 3,655 ms
コンパイル使用メモリ 170,216 KB
実行使用メモリ 30,080 KB
最終ジャッジ日時 2023-08-08 16:05:31
合計ジャッジ時間 12,480 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 236 ms
30,080 KB
testcase_01 AC 7 ms
24,896 KB
testcase_02 AC 7 ms
24,988 KB
testcase_03 AC 7 ms
24,828 KB
testcase_04 AC 7 ms
24,804 KB
testcase_05 AC 7 ms
24,776 KB
testcase_06 AC 7 ms
24,844 KB
testcase_07 AC 514 ms
29,748 KB
testcase_08 AC 523 ms
29,784 KB
testcase_09 AC 523 ms
29,676 KB
testcase_10 AC 511 ms
29,752 KB
testcase_11 AC 507 ms
29,756 KB
testcase_12 AC 508 ms
29,688 KB
testcase_13 AC 516 ms
29,684 KB
testcase_14 AC 521 ms
29,756 KB
testcase_15 AC 534 ms
29,688 KB
testcase_16 AC 527 ms
29,732 KB
testcase_17 AC 526 ms
29,748 KB
testcase_18 AC 514 ms
29,748 KB
testcase_19 AC 530 ms
29,744 KB
testcase_20 AC 520 ms
29,888 KB
testcase_21 AC 526 ms
29,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,Q;
vector<pair<int,int>> E[200005];
int P[21][200005],D[200005];
ll TD[200005];

void dfs(int cur,int pre,ll tot) {
	TD[cur]=tot;
	P[0][cur]=pre;
	FORR(e,E[cur]) if(e.first!=pre) D[e.first]=D[cur]+1, dfs(e.first,cur,tot+e.second);
}
int getpar(int cur,int up) {
	int i;
	FOR(i,20) if(up&(1<<i)) cur=P[i][cur];
	return cur;
}

int lca(int a,int b) {
	int ret=0,i,aa=a,bb=b;
	if(D[aa]>D[bb]) swap(aa,bb);
	for(i=19;i>=0;i--) if(D[bb]-D[aa]>=1<<i) bb=P[i][bb];
	for(i=19;i>=0;i--) if(P[i][aa]!=P[i][bb]) aa=P[i][aa], bb=P[i][bb];
	return (aa==bb)?aa:P[0][aa];               // vertex
}
int dist(int a,int b) {
	int ret=0,i,aa=a,bb=b;
	if(D[aa]>D[bb]) swap(aa,bb);
	for(i=19;i>=0;i--) if(D[bb]-D[aa]>=1<<i) bb=P[i][bb];
	for(i=19;i>=0;i--) if(P[i][aa]!=P[i][bb]) aa=P[i][aa], bb=P[i][bb];
	return D[a]+D[b]-2*D[(aa==bb)?aa:P[0][aa]];  // dist
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N-1) {
		cin>>x>>y>>r;
		E[x].push_back({y,r});
		E[y].push_back({x,r});
	}
	dfs(0,0,0);
	FOR(i,19) FOR(x,N) P[i+1][x]=P[i][P[i][x]];
	
	cin>>Q;
	while(Q--) {
		int a,b,c;
		cin>>a>>b>>c;
		ll tot=0;
		x=lca(a,b); tot+=TD[a]+TD[b]-2*TD[x];
		x=lca(b,c); tot+=TD[b]+TD[c]-2*TD[x];
		x=lca(c,a); tot+=TD[c]+TD[a]-2*TD[x];
		cout<<tot/2<<endl;
	}
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0