結果
| 問題 |
No.439 チワワのなる木
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-29 00:10:39 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,228 bytes |
| コンパイル時間 | 1,399 ms |
| コンパイル使用メモリ | 166,316 KB |
| 実行使用メモリ | 25,856 KB |
| 最終ジャッジ日時 | 2024-11-24 06:47:25 |
| 合計ジャッジ時間 | 2,878 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 9 WA * 19 |
ソースコード
class in{struct myIterator{int it;const bool rev;explicit constexpr myIterator(int it_, bool rev=false):it(it_),rev(rev){}int operator*(){return it;}bool operator!=(myIterator& r){return it!=r.it;}void operator++(){rev?--it:++it;}};const myIterator i,n;public:explicit constexpr in(int n):i(0),n(n){}explicit constexpr in(int i,int n):i(i,n<i),n(n){}const myIterator& begin(){return i;}const myIterator& end(){return n;}};
#include <bits/stdc++.h>
using namespace std;
// input
int n;
string s;
vector<vector<int>> adj;
// end input
using i64 = long long;
struct data {
i64 c, w, ww, wc, cww, wwc;
explicit data() : c(0LL), w(0LL), ww(0LL), wc(0LL), cww(0LL), wwc(0LL){} // 下から上
void print() { cout << c << ' ' << w << ' ' << ww << ' ' << wc << ' ' << cww << ' ' << wwc << endl;}
};
inline void merge(bool c, data& l, const data& r) {
l.c += r.c;
l.w += r.w;
l.ww += r.ww + (!c ? r.w : 0LL);
l.wc += r.wc + (!c ? r.c : 0LL);
l.cww += r.cww + (c ? r.ww: 0LL);
l.wwc += r.wwc + (!c ? r.wc : 0LL);
}
inline void add(data& l, const data& r) {
l.c += r.c;
l.w += r.w;
l.ww += r.ww;
l.wc += r.wc;
l.cww += r.cww;
l.wwc += r.wwc;
}
inline data sa(const data& l_, const data& r) {
data l = l_;
l.c -= r.c;
l.w -= r.w;
l.ww -= r.ww;
l.wc -= r.wc;
l.cww -= r.cww;
l.wwc -= r.wwc;
return l;
}
data ans;
data dfs(int cur, int par) {
data res, sum;
bool c = s[cur] == 'c';
if(c) res.c++; else res.w++;
add(sum, res);
vector<data> dat;
for(int nxt : adj[cur]) {
if(nxt == par) continue;
auto ret = dfs(nxt, cur);
merge(c, res, ret);
dat.emplace_back(ret);
add(sum, ret);
}
/*
for(auto d : dat) {
data r = sa(sum, d);
ans.cww += d.c * r.ww;
ans.cww += d.cw
}
*/
return res;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> n;
cin >> s;
adj.resize(n);
for(int _ : in(n - 1)) {
int a, b;
cin >> a >> b;
--a; --b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
auto result = dfs(0, -1);
cout << result.cww + result.wwc << endl;
}