結果

問題 No.439 チワワのなる木
ユーザー ikdikd
提出日時 2017-11-24 01:57:16
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,153 bytes
コンパイル時間 728 ms
コンパイル使用メモリ 89,040 KB
実行使用メモリ 31,524 KB
最終ジャッジ日時 2023-09-03 16:59:03
合計ジャッジ時間 3,127 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,376 KB
testcase_05 WA -
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
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 176 ms
28,632 KB
testcase_25 AC 119 ms
16,692 KB
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.typecons;

  int n; rd(n);
  auto s=readln.chomp.to!(char[]);
  auto g=new int[][](n, 0);
  foreach(_; 0..(n-1)){
    int a, b; rd(a, b);
    g[a-1]~=(b-1);
    g[b-1]~=(a-1);
  }

  alias T=Tuple!(long, "w", long, "ww");
  auto _w=new long[](n), _ww=new long[](n);  
  T dfs(int i, int pre=-1){
    long w=0, ww=0;
    foreach(j; g[i])if(j!=pre){
      auto res=dfs(j, i);
      w+=res.w; ww+=res.ww;
    }
    if(s[i]=='w'){
      if(w>0) ww+=w;
      w++;
    }
    _w[i]=w; _ww[i]=ww;
    return T(w, ww);
  }

  long sum=0;
  void dfs2(int i, int pre=-1, long w=0, long ww=0){
    if(s[i]=='c') sum+=(ww+_ww[i]);
    // else ww+=w;
    // writeln(i, " ", sum, " ", _w[i], " ", _ww[i], " ", w, " ", ww);
    foreach(j; g[i])if(j!=pre){
      if(s[i]=='c') dfs2(j, i, _w[0]-_w[j], _ww[0]-_ww[j]);
      else dfs2(j, i, w+1, ww+w);
    }
  }

  auto res=dfs(0);
  dfs2(0);
  writeln(sum);
}


void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
0