結果

問題 No.386 貪欲な領主
ユーザー 0w10w1
提出日時 2016-12-15 19:18:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 196 ms / 2,000 ms
コード長 2,211 bytes
コンパイル時間 2,084 ms
コンパイル使用メモリ 172,636 KB
実行使用メモリ 23,900 KB
最終ジャッジ日時 2023-08-20 07:40:45
合計ジャッジ時間 3,985 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 196 ms
23,792 KB
testcase_05 AC 152 ms
17,916 KB
testcase_06 AC 162 ms
17,624 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 19 ms
4,552 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 6 ms
4,380 KB
testcase_14 AC 161 ms
17,640 KB
testcase_15 AC 173 ms
23,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef unsigned long long ull;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< string > vs;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

const int INF = 0x3f3f3f3f;

int N;
vvi G;
vi U;

void init(){
  cin >> N;
  G = vvi( N );
  for( int i = 0; i < N - 1; ++i ){
    int A, B; cin >> A >> B;
    G[ A ].emplace_back( B );
    G[ B ].emplace_back( A );
  }
  U = vi( N );
  for( int i = 0; i < N; ++i )
    cin >> U[ i ];
}

vi cost2root;
vvi par;
vi depth;

void dfs( int u, int fa, int cost, int dpt ){
  par[ 0 ][ u ] = fa;
  cost2root[ u ] = cost + U[ u ];
  depth[ u ] = dpt;
  for( int v : G[ u ] ){
    if( v == fa ) continue;
    dfs( v, u, cost2root[ u ], dpt + 1 );
  }
}

void preprocess(){
  cost2root = vi( N );
  par = vvi( 20, vi( N, -1 ) );
  depth = vi( N );
  dfs( 0, -1, 0, 0 );
  for( int i = 0; i + 1 < 20; ++i )
    for( int j = 0; j < N; ++j ){
      int p = par[ i ][ j ];
      if( p == -1 ) continue;
      par[ i + 1 ][ j ] = par[ i ][ p ];
    }
}

int get_anc( int u, int v ){
  if( not ( depth[ u ] >= depth[ v ] ) )
    swap( u, v );
  int diff = depth[ u ] - depth[ v ];
  for( int i = 0; i < 20; ++i )
    if( diff & 1 << i )
      u = par[ i ][ u ],
      diff -= 1 << i;
  if( u == v )
    return u;
  for( int i = 20 - 1; i >= 0; --i ){
    int p = par[ i ][ u ];
    int q = par[ i ][ v ];
    if( p != q )
      u = p, v = q;
  }
  return par[ 0 ][ u ];
}

void solve(){
  int M; cin >> M;
  ull ans = 0;
  for( int i = 0; i < M; ++i ){
    int A, B, C; cin >> A >> B >> C;
    int anc = get_anc( A, B );
    ull cost = cost2root[ A ] + cost2root[ B ] - 2 * cost2root[ anc ] + U[ anc ];
    ans += cost * C;
  }
  cout << ans << endl;
}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}
0