結果

問題 No.386 貪欲な領主
ユーザー ChanyuhChanyuh
提出日時 2020-02-26 21:27:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 433 ms / 2,000 ms
コード長 2,944 bytes
コンパイル時間 1,404 ms
コンパイル使用メモリ 117,036 KB
実行使用メモリ 41,752 KB
最終ジャッジ日時 2024-04-21 16:34:35
合計ジャッジ時間 4,462 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 433 ms
41,752 KB
testcase_05 AC 228 ms
28,136 KB
testcase_06 AC 242 ms
28,032 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 23 ms
5,632 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 AC 2 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 238 ms
28,032 KB
testcase_15 AC 365 ms
41,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

struct LCA{
    int n,root,k=0;
    vector<vector<int>> D,G;
    vector<int> depth;

    void dfs(int s,int par){
        D[s].push_back(par);
        if (par!=-1) depth[s]=depth[par]+1;
        rep(i,G[s].size()){
            int t=G[s][i];
            if (t==par) continue;
            dfs(t,s);
        }
    }
    
    LCA(int r,vector<vector<int>> G_){
        n=G_.size();
        G=G_;
        root=r;
        depth.resize(n,0);
        D.resize(n,{});
        dfs(root,-1);
        while(true){
            bool flag=false;
            int m=(1<<k);
            rep(i,n){
                if (D[i][k]==-1) D[i].push_back(-1);
                else{
                    if (D[D[i][k]][k]!=-1) flag=true;
                    D[i].push_back(D[D[i][k]][k]);
                }
            }
            if (!flag) break;
            k+=1;
        }
    }

    int lca(int p,int q){
        if (depth[p]<depth[q]) swap(p,q);
        per(i,k+1){
            if (((depth[p]-depth[q]) & (1 << i))!=0) p=D[p][i];
        }
        if (p==q) return p;
        per(i,k+1){
            if (D[p][i]!=D[q][i]) {
                p=D[p][i];
                q=D[q][i];
            }
        }
        return D[p][0];
    }

    
};

int n,m;
vector<vector<int>> G;
ll u[100010],S[100010];


void dfs(int s,int par,ll d){
  S[s]=u[s]+d;
  for(int t:G[s]){
    if(t==par) continue;
    dfs(t,s,S[s]);
  }
}

void solve(){
  cin >> n;
  G.resize(n,vector<int>(0));
  rep(i,n-1){
    int a,b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  
  LCA lca(0,G);
  rep(i,n){
    cin >> u[i];
  }
  dfs(0,-1,0);
  ll ans=0;
  cin >> m;
  rep(i,m){
    int a,b,c;cin >> a >> b >> c;
    //cout << lca.lca(a,b) << endl;
    //cout << c*(S[a]+S[b]-2*S[lca.lca(a,b)])+u[lca.lca(a,b)] << endl;
    ans+=c*(S[a]+S[b]-2*S[lca.lca(a,b)]+u[lca.lca(a,b)]);
  }
  cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0