結果

問題 No.386 貪欲な領主
ユーザー koyopro
提出日時 2020-01-09 22:41:34
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 294 ms / 2,000 ms
コード長 2,546 bytes
コンパイル時間 2,737 ms
コンパイル使用メモリ 165,112 KB
実行使用メモリ 42,624 KB
最終ジャッジ日時 2024-11-23 05:01:53
合計ジャッジ時間 6,318 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) printf(__VA_ARGS__)
int dxy[] = {0, 1, 0, -1, 0};
void solve();
signed main()
{
#if DEBUG
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
#endif
cin.tie(0);
ios::sync_with_stdio(false);
solve();
return 0;
}
/*================================*/
int N,M,Q;
/**********************************
*
* Graph
*
**********************************/
struct Edge {
int src, to, w;
Edge(int u, int v, int w): src(u), to(v), w(w) {};
};
vector< vector<Edge> > E(111111);
int W[111111];
int U[111111];
int D[111111];
int P[111111][20];
void doubling() {
FOR(j, 1, 20) {
REP(i, N) {
P[i][j] = (P[i][j-1] == -1) ? -1 : P[P[i][j-1]][j-1];
}
}
}
void add_edge(int u, int v, int w) {
E[u].push_back(*new Edge(u,v,w));
E[v].push_back(*new Edge(v,u,w));
}
void dfs(int parent=-1, int i=0, int weight=0, int depth=0) {
P[i][0] = parent;
W[i] = weight+U[i];
// out("%lld: %lld -> %lld\n", i, U[i], W[i]);
D[i] = depth;
for (auto e : E[i]) {
if (e.to == parent) continue;
dfs(i, e.to, W[i], depth+1);
}
}
int lca(int u, int v) {
// depth調
if (D[u] > D[v]) swap(u, v);
RFOR(i, 0, 20) {
if ((D[v] - D[u])&(1<<i)) v = P[v][i];
}
if (u == v) return u;
// lca
RFOR(i, 0, 20) {
if (P[v][i] != P[u][i]) {
v = P[v][i];
u = P[u][i];
}
}
return P[u][0];
}
int route_weight(int u, int v) {
return W[u] + W[v] - W[lca(u, v)] * 2;
}
void setup_tree() {
REP(i,N)REP(j,20) P[i][j]=-1;
dfs();
doubling();
}
/**********************************************************/
void solve() {
cin>>N;
int a,b,c;
REP(i,N-1) {
cin>>a>>b;
add_edge(a, b, 0);
}
REP(i,N) cin>>U[i];
setup_tree();
cin >> M;
int ans = 0;
REP(i,M) {
cin>>a>>b>>c;
int l = lca(a,b);
ans += (W[a]+W[b]-2*W[l]+U[l])*c;
}
cout << ans << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0