結果

問題 No.439 チワワのなる木
ユーザー te-shte-sh
提出日時 2017-10-30 16:46:19
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 120 ms / 5,000 ms
コード長 1,232 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 91,780 KB
実行使用メモリ 14,076 KB
最終ジャッジ日時 2023-09-03 16:38:21
合計ジャッジ時間 2,846 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 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,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 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,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 89 ms
11,832 KB
testcase_19 AC 84 ms
12,060 KB
testcase_20 AC 114 ms
12,956 KB
testcase_21 AC 31 ms
5,668 KB
testcase_22 AC 29 ms
5,208 KB
testcase_23 AC 120 ms
13,360 KB
testcase_24 AC 118 ms
13,064 KB
testcase_25 AC 102 ms
14,076 KB
testcase_26 AC 97 ms
13,712 KB
testcase_27 AC 78 ms
14,072 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 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;
        q.insertBack(v);
      }
  }

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

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

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

  writeln(ans);
}
0