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