結果
| 問題 |
No.386 貪欲な領主
|
| コンテスト | |
| ユーザー |
h_noson
|
| 提出日時 | 2016-07-02 01:11:08 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 197 ms / 2,000 ms |
| コード長 | 1,721 bytes |
| コンパイル時間 | 610 ms |
| コンパイル使用メモリ | 68,060 KB |
| 実行使用メモリ | 31,656 KB |
| 最終ジャッジ日時 | 2024-10-12 19:42:35 |
| 合計ジャッジ時間 | 2,352 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:55:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
55 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
main.cpp:58:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
58 | scanf("%d%d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:62:20: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
62 | rep (i,n) scanf("%d",&u[i]);
| ~~~~~^~~~~~~~~~~~
main.cpp:70:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
70 | scanf("%d",&m);
| ~~~~~^~~~~~~~~
main.cpp:74:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
74 | scanf("%d%d%d",&a,&b,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
ソースコード
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
#define REP(i,s,e) for (i = s; i <= e; i++)
#define rep(i,n) REP (i,0,(int)(n)-1)
#define RREP(i,s,e) for (i = s; i >= e; i--)
#define rrep(i,n) RREP (i,(int)(n)-1,0)
#define INF (int)1e8
#define MOD (int)(1e9+7)
#define MAX 100000
typedef long long ll;
vector<int> e[MAX];
int u[MAX];
int parent[20][MAX];
int dist[20][MAX];
int depth[MAX];
void dfs(int v, int p, int d) {
parent[0][v] = p;
depth[v] = d;
dist[0][v] = u[v];
for (auto& x : e[v]) if (parent[0][v] != x) {
dfs(x,v,d+1);
}
}
int query(int a, int b) {
int i, ret = 0;
if (depth[a] > depth[b]) swap(a,b);
rep (i,20) {
if ((depth[b] - depth[a]) >> i & 1) {
ret += dist[i][b];
b = parent[i][b];
}
}
if (a == b) return ret + u[a];
rrep (i,20) {
if (parent[i][a] != parent[i][b]) {
ret += dist[i][a] + dist[i][b];
a = parent[i][a];
b = parent[i][b];
}
}
return ret + u[a] + u[b] + u[parent[0][a]];
}
int main(void) {
int i, j, n, m;
scanf("%d",&n);
rep (i,n-1) {
int a, b;
scanf("%d%d",&a,&b);
e[a].push_back(b);
e[b].push_back(a);
}
rep (i,n) scanf("%d",&u[i]);
dfs(0,-1,0);
for (i = 1; i < 20; i++) rep (j,n) {
parent[i][j] = parent[i][j] >= 0 ? parent[i-1][parent[i-1][j]] : -1;
dist[i][j] = dist[i-1][parent[i-1][j]] + dist[i-1][j];
}
scanf("%d",&m);
ll ans = 0;
while (m--) {
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
ans += query(a,b) * c;
}
printf("%lld\n",ans);
return 0;
}
h_noson