結果
| 問題 |
No.439 チワワのなる木
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-11-01 18:24:36 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 117 ms / 5,000 ms |
| コード長 | 1,322 bytes |
| コンパイル時間 | 987 ms |
| コンパイル使用メモリ | 84,328 KB |
| 実行使用メモリ | 27,520 KB |
| 最終ジャッジ日時 | 2024-11-25 01:05:26 |
| 合計ジャッジ時間 | 3,378 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
typedef long long ll;
using namespace std;
int main() {
int n; string s; cin >> n >> s;
vector<vector<int> > g(n);
repeat (i,n-1) {
int a, b; cin >> a >> b; -- a; -- b;
g[a].push_back(b);
g[b].push_back(a);
}
int total_c = whole(count, s, 'c');
int total_w = whole(count, s, 'w');
ll ans = 0;
function<pair<int, int> (int, int)> go = [&](int i, int parent) {
vector<int> cs, ws;
for (int j : g[i]) if (j != parent) {
int c, w; tie(c, w) = go(j, i);
cs.push_back(c);
ws.push_back(w);
}
int acc_c = whole(accumulate, cs, 0) + (s[i] == 'c');
int acc_w = whole(accumulate, ws, 0) + (s[i] == 'w');
if (s[i] == 'w') {
cs.push_back(total_c - acc_c);
ws.push_back(total_w - acc_w);
repeat (j, cs.size()) {
ans += cs[j] *(ll) (total_w-1 - ws[j]);
}
}
return make_pair(acc_c, acc_w);
};
go(0, -1);
cout << ans << endl;
return 0;
}