結果

問題 No.439 チワワのなる木
ユーザー ikdikd
提出日時 2017-11-24 00:20:16
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 795 bytes
コンパイル時間 881 ms
コンパイル使用メモリ 89,032 KB
実行使用メモリ 8,768 KB
最終ジャッジ日時 2023-09-03 16:58:26
合計ジャッジ時間 8,144 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,768 KB
testcase_01 AC 1 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,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 12 ms
4,376 KB
testcase_16 AC 44 ms
4,380 KB
testcase_17 AC 24 ms
4,380 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

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");
  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++;
    }
    return T(w, ww);
  }

  long sum=0;
  foreach(i; 0..n)if(s[i]=='c'){
    sum+=dfs(i).ww;
  }

  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