結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
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 169 ms
23,400 KB
testcase_25 AC 120 ms
15,232 KB
testcase_26 WA -
testcase_27 AC 111 ms
25,188 KB
権限があれば一括ダウンロードができます

ソースコード

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[i]+ww);
    else{
      if(w>0) ww+=w;
      w++;
    }
    foreach(j; g[i])if(j!=pre){
      dfs2(j, i, w, ww);
    }
  }

  auto tmp=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