結果

問題 No.439 チワワのなる木
ユーザー 0w10w1
提出日時 2017-01-07 17:28:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 82 ms / 5,000 ms
コード長 1,968 bytes
コンパイル時間 1,591 ms
コンパイル使用メモリ 169,276 KB
実行使用メモリ 19,592 KB
最終ジャッジ日時 2023-08-22 16:20:12
合計ジャッジ時間 4,764 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 41 ms
9,436 KB
testcase_19 AC 36 ms
9,240 KB
testcase_20 AC 67 ms
11,188 KB
testcase_21 AC 14 ms
5,704 KB
testcase_22 AC 14 ms
5,476 KB
testcase_23 AC 70 ms
12,588 KB
testcase_24 AC 82 ms
18,268 KB
testcase_25 AC 46 ms
12,264 KB
testcase_26 AC 44 ms
11,972 KB
testcase_27 AC 45 ms
19,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
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;
string seq;

vvi G;

void init(){
  cin >> N;
  cin >> seq;
  G = vvi( N );
  for( int i = 0; i < N - 1; ++i ){
    int u, v; cin >> u >> v;
    G[ u - 1 ].emplace_back( v - 1 );
    G[ v - 1 ].emplace_back( u - 1 );
  }
}

const int MAXN = ( int ) 1e5;

ll dp_d[ MAXN + 1 ][ 2 ]; // how many "ww"s / how many "w"s
ll dp_u[ MAXN + 1 ][ 2 ]; 

void dfs_d( int u, int fa ){
  ll ww = 0, w = 0;
  for( int v : G[ u ] ){
    if( v == fa ) continue;
    dfs_d( v, u );
    ww += dp_d[ v ][ 0 ];
    w += dp_d[ v ][ 1 ];
  }
  if( seq[ u ] == 'w' ){
    ww += w;
    ++w;
  }
  dp_d[ u ][ 0 ] = ww;
  dp_d[ u ][ 1 ] = w;
}

void dfs_u( int u, int fa, ll upww, ll upw ){
  dp_u[ u ][ 0 ] = upww;
  dp_u[ u ][ 1 ] = upw;
  for( int v : G[ u ] ){
    if( v == fa ) continue;
    upww += dp_d[ v ][ 0 ];
    upw += dp_d[ v ][ 1 ];
  }
  for( int v : G[ u ] ){
    if( v == fa ) continue;
    ll nupww = upww - dp_d[ v ][ 0 ];
    ll nupw = upw - dp_d[ v ][ 1 ];
    if( seq[ u ] == 'w' ){
      nupww += nupw;
      ++nupw;
    }
    dfs_u( v, u, nupww, nupw );
  }
}

void preprocess(){
  dfs_d( 0, -1 );
  dfs_u( 0, -1, 0, 0 );
}

void solve(){
  ll ans = 0;
  for( int i = 0; i < N; ++i ){
    if( seq[ i ] == 'c' ){
      ans += dp_d[ i ][ 0 ] + dp_u[ i ][ 0 ];
    }
  }
  cout << ans << endl;
}

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