結果

問題 No.386 貪欲な領主
ユーザー HAHAHAHAHAHA
提出日時 2016-09-16 15:21:50
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,276 bytes
コンパイル時間 1,401 ms
コンパイル使用メモリ 165,988 KB
実行使用メモリ 25,696 KB
最終ジャッジ日時 2024-04-28 14:36:25
合計ジャッジ時間 3,523 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 205 ms
25,644 KB
testcase_05 AC 148 ms
18,048 KB
testcase_06 AC 159 ms
17,764 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 20 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 RE -
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 157 ms
17,916 KB
testcase_15 AC 181 ms
25,696 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:19: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘int*’ [-Wformat=]
   58 |         scanf("%lld",&n);
      |                ~~~^  ~~
      |                   |  |
      |                   |  int*
      |                   long long int*
      |                %d
main.cpp:68:39: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type*’ {aka ‘int*’} [-Wformat=]
   68 |         for(ll i=0;i<n;i++) scanf("%lld", &U[i]);
      |                                    ~~~^
      |                                       |
      |                                       long long int*
      |                                    %d
main.cpp:58:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   58 |         scanf("%lld",&n);
      |         ~~~~~^~~~~~~~~~~
main.cpp:62:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   62 |                 scanf("%lld%lld", &a,&b);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp:68:34: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   68 |         for(ll i=0;i<n;i++) scanf("%lld", &U[i]);
      |                             ~~~~~^~~~~~~~~~~~~~~
main.cpp:73:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   73 |         scanf("%lld",&Q);
      |         ~~~~~^~~~~~~~~~~
main.cpp:77:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   77 |                 scanf("%lld%lld%lld", &

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MAX_N = 100100;
const int MAX_LOG= 20;
int n;

int par[MAX_LOG][MAX_N];
int depth[MAX_N];
int cost[MAX_N];
vector<vector<int> > G;
vector<int> U;


void dfs(ll v,ll p, ll d, ll c){
	par[0][v]=p;
	depth[v]=d;
	cost[v]=c+U[v];
	for(auto nxt : G[v]){
		if(nxt !=p)
			dfs(nxt, v, d+1, c+U[v]);
	}
}

void setPar(){
	dfs(0,-1,0,0);
	for(ll i=0;i<MAX_LOG-1;i++){
		for(ll j=0;j<n;j++){
			if(par[i][j]== -1)
				par[i+1][j]= -1;
			else
				par[i+1][j]=par[i][par[i][j]];
		}
	}
}


ll lca(ll a,ll b){
	if(depth[a]>depth[b])
		swap(a,b);
	for(ll i=0;i<MAX_LOG;i++){
		if((depth[b]-depth[a]) >> i&1)
			b=par[i][b];
	}
	if(a==b) return a;
	
	for(ll i=MAX_LOG-1;i>=0;i--){
		if(par[i][a]!=par[i][b]){
			a=par[i][a];
			b=par[i][b];
		}
	}
	return par[0][a];
}

int main(){
	scanf("%lld",&n);
	G.resize(n);
	for(ll i=0;i<n-1;i++){
		ll a,b;
		scanf("%lld%lld", &a,&b);
		G[a].push_back(b);
		G[b].push_back(a);
	}
	
	U.resize(n);
	for(ll i=0;i<n;i++) scanf("%lld", &U[i]);
	
	setPar();
	
	ll Q;
	scanf("%lld",&Q);
	ll ans=0;
	for(ll i=0;i<Q;i++){
		ll a,b,m;
		scanf("%lld%lld%lld", &a,&b,&m);
		ll c=lca(a,b);
		ans+=(cost[a] + cost[b] - 2 * cost[c] + U[c]) * m;
	}
	
	printf("%lld\n", ans);
	return 0;
}
0