結果
| 問題 |
No.439 チワワのなる木
|
| コンテスト | |
| ユーザー |
mayoko_
|
| 提出日時 | 2016-04-28 21:31:20 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,094 bytes |
| コンパイル時間 | 1,849 ms |
| コンパイル使用メモリ | 172,492 KB |
| 実行使用メモリ | 12,288 KB |
| 最終ジャッジ日時 | 2024-10-11 00:13:43 |
| 合計ジャッジ時間 | 8,733 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 TLE * 1 -- * 9 |
ソースコード
// とりあえず TLE 解
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
int N;
string s;
vector<vector<int>> G;
// first: ww second: w
pll dfs(int v, int p) {
pll ret;
if (s[v] == 'w') ret.second = 1;
for (int ch : G[v]) if (ch != p) {
pll tmp = dfs(ch, v);
ret.first += tmp.first;
ret.second += tmp.second;
if (s[v] == 'w') ret.first += tmp.second;
}
return ret;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> N;
cin >> s;
for (int i = 0; i < N; i++) assert(s[i] == 'c' || s[i] == 'w');
G.resize(N);
for (int i = 0; i < N-1; i++) {
int a, b;
cin >> a >> b;
a--; b--;
G[a].emplace_back(b);
G[b].emplace_back(a);
}
// すべての c から木DP
ll ans = 0;
for (int i = 0; i < N; i++) {
if (s[i] == 'c') {
pll tmp = dfs(i, -1);
ans += tmp.first;
}
}
cout << ans << endl;
}
mayoko_