結果
| 問題 | No.386 貪欲な領主 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-06-28 13:39:11 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 203 ms / 2,000 ms |
| コード長 | 1,665 bytes |
| コンパイル時間 | 443 ms |
| コンパイル使用メモリ | 53,280 KB |
| 実行使用メモリ | 29,568 KB |
| 最終ジャッジ日時 | 2024-10-12 03:48:04 |
| 合計ジャッジ時間 | 2,274 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:75:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
75 | scanf("%d", &V);
| ~~~~~^~~~~~~~~~
main.cpp:78:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
78 | scanf("%d %d", &v, &u);
| ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:83:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
83 | scanf("%d", &U[i]);
| ~~~~~^~~~~~~~~~~~~
main.cpp:88:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
88 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
main.cpp:92:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
92 | scanf("%d %d %d", &u, &v, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <cstdio>
#include <cstring>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAX_V = 100010;
const int MAX_LOG_V = 20;
int V;
vector<int> G[MAX_V];
int U[MAX_V];
int parent[MAX_LOG_V][MAX_V];
int sum[MAX_LOG_V][MAX_V];
int depth[MAX_V];
void dfs(int v, int p, int d){
parent[0][v] = p;
sum[0][v] = U[v];
depth[v] = d;
for(int u : G[v]){
if(u != p){
dfs(u, v, d + 1);
}
}
}
void init(){
dfs(0, -1, 0);
for(int k = 0; k + 1 < MAX_LOG_V; k++){
for(int v = 0; v < V; v++){
if(parent[k][v] < 0){
parent[k + 1][v] = -1;
sum[k + 1][v] = sum[k][v];
} else {
int p = parent[k][v];
parent[k + 1][v] = parent[k][p];
sum[k + 1][v] = sum[k][v] + sum[k][p];
}
}
}
}
int solve(int u, int v){
int res = 0;
if(depth[u] > depth[v])swap(u, v);
for(int k = 0; k < MAX_LOG_V; k++){
if((depth[v] - depth[u]) >> k & 1){
res += sum[k][v];
v = parent[k][v];
}
}
if(u == v){
return res + U[u];
}
for(int k = MAX_LOG_V - 1; k >= 0; k--){
if(parent[k][u] != parent[k][v]){
res += sum[k][u] + sum[k][v];
u = parent[k][u];
v = parent[k][v];
}
}
int lca = parent[0][u];
res += U[u] + U[v] + U[lca];
return res;
}
int main(){
scanf("%d", &V);
for(int i=0;i<V-1;i++){
int v, u;
scanf("%d %d", &v, &u);
G[v].push_back(u);
G[u].push_back(v);
}
for(int i=0;i<V;i++){
scanf("%d", &U[i]);
}
init();
int q;
scanf("%d", &q);
ll res = 0;
for(int i=0;i<q;i++){
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
res += solve(u, v) * w;
}
printf("%lld\n", res);
return 0;
}