結果

問題 No.439 チワワのなる木
ユーザー beetbeet
提出日時 2018-12-04 21:24:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 133 ms / 5,000 ms
コード長 1,178 bytes
コンパイル時間 2,323 ms
コンパイル使用メモリ 207,392 KB
実行使用メモリ 22,504 KB
最終ジャッジ日時 2023-09-22 14:09:01
合計ジャッジ時間 4,263 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 69 ms
8,544 KB
testcase_19 AC 65 ms
8,440 KB
testcase_20 AC 96 ms
10,148 KB
testcase_21 AC 24 ms
5,520 KB
testcase_22 AC 23 ms
4,976 KB
testcase_23 AC 119 ms
11,788 KB
testcase_24 AC 133 ms
20,780 KB
testcase_25 AC 84 ms
10,908 KB
testcase_26 AC 81 ms
11,092 KB
testcase_27 AC 86 ms
22,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
signed main(){
  Int n;
  string s;
  cin>>n>>s;
  vector<vector<Int> > G(n);
  for(Int i=1;i<n;i++){
    Int x,y;
    cin>>x>>y;
    x--;y--;
    G[x].emplace_back(y);
    G[y].emplace_back(x);
  }
  vector<Int> w(n,0),ww(n,0);
  {
    function<void(Int, Int)> dfs=
      [&](Int v,Int p){
        w[v]=s[v]=='w';
        for(Int u:G[v]){
          if(u==p) continue;
          dfs(u,v);
          w[v]+=w[u];
          ww[v]+=ww[u];
          if(s[v]=='w') ww[v]+=w[u];
        }        
      };
    dfs(0,-1);
  }
  Int ans=0,wa=0;
  for(Int i=0;i<n;i++) wa+=s[i]=='w';
  function<void(Int, Int, Int)> dfs=
    [&](Int v,Int p,Int x){
      //cout<<v<<":"<<x<<" "<<ww[v]<<endl;
      if(s[v]=='c') ans+=x+ww[v];
      for(Int u:G[v]){
        if(u==p) continue;
        if(s[v]=='c') dfs(u,v,x+(ww[v]-ww[u]));
        else dfs(u,v,x+(ww[v]-(w[u]+ww[u]))+(wa-w[v]));
      }
    };
  dfs(0,-1,0);
  cout<<ans<<endl;
  return 0;
}
0