結果
| 問題 |
No.439 チワワのなる木
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-10-29 16:15:25 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,452 bytes |
| コンパイル時間 | 1,868 ms |
| コンパイル使用メモリ | 168,108 KB |
| 実行使用メモリ | 16,512 KB |
| 最終ジャッジ日時 | 2024-11-24 18:01:26 |
| 合計ジャッジ時間 | 2,848 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 11 WA * 17 |
ソースコード
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;
i64 cSum, wSum, ans;
pair<i64/*c*/, i64/*w*/> dfs(int cur, int par) {
i64 partC = 0LL, partW = 0LL;
bool is_w = s[cur] == 'w';
for(int nv : adj[cur]) {
if(nv == par) continue;
i64 c, w;
tie(c, w) = dfs(nv, cur);
partC += c;
partW += w;
}
if(is_w) {
ans += partW * (cSum - partC);
ans += partC * (wSum - partW - 1LL);
}
if(is_w) partW++; else partC++;
return make_pair(partC, partW);
}
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);
}
ans = cSum = wSum = 0LL;
for(char c : s) {
if(c == 'c') ++cSum;
else ++wSum;
}
dfs(0, -1);
cout << ans << endl;
}