結果

問題 No.386 貪欲な領主
ユーザー 👑 kmjpkmjp
提出日時 2016-07-01 23:07:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 1,583 bytes
コンパイル時間 1,389 ms
コンパイル使用メモリ 162,372 KB
実行使用メモリ 35,388 KB
最終ジャッジ日時 2024-04-20 08:28:27
合計ジャッジ時間 3,522 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
23,948 KB
testcase_01 AC 6 ms
23,376 KB
testcase_02 AC 6 ms
23,532 KB
testcase_03 AC 7 ms
23,636 KB
testcase_04 AC 298 ms
35,388 KB
testcase_05 AC 210 ms
28,312 KB
testcase_06 AC 224 ms
29,852 KB
testcase_07 AC 8 ms
24,680 KB
testcase_08 AC 28 ms
23,732 KB
testcase_09 AC 10 ms
23,656 KB
testcase_10 AC 7 ms
26,388 KB
testcase_11 AC 7 ms
24,704 KB
testcase_12 AC 8 ms
22,980 KB
testcase_13 AC 10 ms
23,008 KB
testcase_14 AC 207 ms
29,736 KB
testcase_15 AC 241 ms
34,684 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,M;
vector<int> E[200005];
int P[21][200005],D[200005];
int U[202020];
int T[202020];
ll ret;

void dfs(int cur) {
	T[cur]+=U[cur];
	ITR(it,E[cur]) if(*it!=P[0][cur]) D[*it]=D[cur]+1,T[*it]=T[cur],P[0][*it]=cur, dfs(*it);
}
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
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N-1) {
		cin>>x>>y;
		E[x].push_back(y);
		E[y].push_back(x);
	}
	FOR(i,N) cin>>U[i];
	dfs(0);
	FOR(i,19) FOR(x,N) P[i+1][x]=P[i][P[i][x]];
	
	cin>>M;
	while(M--) {
		cin>>x>>y>>i;
		int lc=lca(x,y);
		int tot=T[x]-T[lc]+T[y]-T[lc]+U[lc];
		ret += i*tot;
	}
	cout<<ret<<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);
	solve(); return 0;
}
0