結果

問題 No.439 チワワのなる木
ユーザー beetbeet
提出日時 2018-12-04 21:20:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,159 bytes
コンパイル時間 1,986 ms
コンパイル使用メモリ 207,452 KB
実行使用メモリ 22,616 KB
最終ジャッジ日時 2023-09-22 11:41:15
合計ジャッジ時間 4,970 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 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,376 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 2 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 108 ms
20,744 KB
testcase_25 AC 78 ms
10,960 KB
testcase_26 WA -
testcase_27 AC 83 ms
22,616 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+(wa-(w[u]+1)));
      }
    };
  dfs(0,-1,0);
  cout<<ans<<endl;
  return 0;
}
0