結果
| 問題 |
No.439 チワワのなる木
|
| ユーザー |
|
| 提出日時 | 2017-10-30 15:34:16 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,400 bytes |
| コンパイル時間 | 815 ms |
| コンパイル使用メモリ | 106,264 KB |
| 実行使用メモリ | 14,492 KB |
| 最終ジャッジ日時 | 2024-06-12 22:16:12 |
| 合計ジャッジ時間 | 2,935 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 WA * 18 |
ソースコード
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);
}