結果

問題 No.439 チワワのなる木
ユーザー te-shte-sh
提出日時 2017-10-30 15:34:16
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,400 bytes
コンパイル時間 1,949 ms
コンパイル使用メモリ 91,736 KB
実行使用メモリ 15,004 KB
最終ジャッジ日時 2023-09-03 16:37:54
合計ジャッジ時間 5,213 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 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 AC 1 ms
4,376 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 132 ms
13,880 KB
testcase_25 AC 117 ms
15,004 KB
testcase_26 WA -
testcase_27 AC 79 ms
13,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap

void main()
{
  auto n = readln.chomp.to!int;
  auto s = readln.chomp;

  auto children = new int[][](n);
  foreach (_; 0..n-1) {
    auto rd = readln.splitter;
    auto a = rd.front.to!int-1; rd.popFront();
    auto b = rd.front.to!int-1;
    children[a] ~= b;
    children[b] ~= a;
  }

  auto parent = new int[](n);
  parent[0] = -1;

  auto ac = new int[](n), aw = new int[](n);

  auto q = DList!int(0), st = SList!int();
  while (!q.empty) {
    auto u = q.front; q.removeFront();
    st.insertFront(u);
    foreach (v; children[u])
      if (v != parent[u]) {
        parent[v] = u;
        ac[v] = ac[u] + (s[u] == 'c');
        aw[v] = aw[u] + (s[u] == 'w');
        q.insertBack(v);
      }
  }

  auto dc = new int[](n), dw = new int[](n);

  while (!st.empty) {
    auto u = st.front; st.removeFront();
    foreach (v; children[u])
      if (v != parent[u]) {
        dc[u] += dc[v] + (s[v] == 'c');
        dw[u] += dw[v] + (s[v] == 'w');
      }
  }

  auto ans = 0L;
  foreach (u; 0..n)
    if (s[u] == 'w') {
      ans += long(ac[u]) * dw[u];
      ans += long(aw[u]) * dc[u];
      ans += long(dc[u]) * dw[u];
      foreach (v; children[u])
        if (v != parent[u])
          ans -= long(dc[v] + (s[v] == 'c')) * (dw[v] + (s[v] == 'w'));
    }

  writeln(ans);
}
0