結果

問題 No.386 貪欲な領主
ユーザー okadukiokaduki
提出日時 2016-07-01 23:28:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,518 bytes
コンパイル時間 1,601 ms
コンパイル使用メモリ 168,572 KB
実行使用メモリ 15,872 KB
最終ジャッジ日時 2024-04-20 20:58:41
合計ジャッジ時間 2,902 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 3 ms
6,144 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 3 ms
6,016 KB
testcase_11 AC 3 ms
5,888 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

struct Edge{
  int to, cost;

  Edge(int t, int c): to(t), cost(c)
  {}
  bool operator>(const Edge& rhs) const{
	return cost > rhs.cost;
  }
  bool operator<(const Edge& rhs) const{
	return cost < rhs.cost;
  }

};
typedef vector< vector<Edge> > Graph;

// calc Lowest Common Ancestor
// O(N logN)
int N;
const int MAX = 100000;
const int LOG_MAX = log2(MAX);
int depth[MAX];
int parent[LOG_MAX][MAX];
Graph G(MAX);
LL cost[MAX];
LL sumcost[MAX];
void dfs(int node, int p = -1, int d = 0)
{
  if(depth[node] >= 0) return;
  depth[node] = d;
  parent[0][node] = p;
  sumcost[node] = (p>=0?sumcost[p]:0) + cost[node];
  for(int i=0;i<SZ(G[node]);++i)
	dfs(G[node][i].to, node, d+1);
}

void init(){
  fill(depth, depth+MAX, -1);
  dfs(0);
  for(int i=0;i+1<LOG_MAX;++i){
	for(int v=0;v<N;++v){
	  if(parent[i][v] == -1) parent[i+1][v] = -1;
	  else parent[i+1][v] = parent[i][parent[i][v]];
	}
  }
}

int LCA(int u, int v){
  if(depth[u] > depth[v]) swap(u, v);
  //depth[u] <= depth[v]
  for(int i=0;i<LOG_MAX;++i)
	if((depth[v] - depth[u]) >> i & 1)
	  v = parent[i][v];
  if(u == v) return u;

  for(int i=LOG_MAX-1;i>=0;--i){
	if(parent[i][u] != parent[i][v]){
	  u = parent[i][u];
	  v = parent[i][v];
	}
  }
  return parent[0][u];
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N; cin >> N;
  REP(i,N-1){
	int u, v; cin >> u >> v;
	G[u].EB(v,1);
	G[v].EB(u,1);
  }
  REP(i,N) cin >> cost[i];
  init();

  int M; cin >> M;
  LL ans = 0;
  REP(i,M){
	int a, b, c; cin >> a >> b >> c;
	int u = LCA(a,b);
	ans += c*(sumcost[a] + sumcost[b] - sumcost[u]*2 + cost[u]);
	//cout << a<<","<<b<<" -> "<<LCA(a,b)<<endl;
  }
  cout << ans << endl;
  
  return 0;
}
0