結果
問題 | No.439 チワワのなる木 |
ユーザー |
|
提出日時 | 2019-12-25 18:23:36 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 71 ms / 5,000 ms |
コード長 | 1,690 bytes |
コンパイル時間 | 1,038 ms |
コンパイル使用メモリ | 107,164 KB |
実行使用メモリ | 11,264 KB |
最終ジャッジ日時 | 2024-10-02 03:32:40 |
合計ジャッジ時間 | 2,964 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 28 |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> #include <limits> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208; int main() { int n; cin >> n; string S; cin >> S; vector<vector<int>> G(n); for (int i = 0; i < n-1; ++i) { int a, b; scanf("%d %d", &a, &b); a--; b--; G[a].emplace_back(b); G[b].emplace_back(a); } vector<int> cs(n), ws(n); ll csum = 0, wsum = 0; for (int i = 0; i < n; ++i) { if(S[i] == 'c') cs[i]++, csum++; else ws[i]++, wsum++; } ll ans = csum*wsum*(wsum-1); deque<int> Q; stack<int> s; int cnt = 0; vector<int> visited(n, 0), num(n); s.emplace(0); while(!s.empty()){ int a = s.top(); s.pop(); visited[a]++; num[a] = cnt++; Q.emplace_front(a); for (auto &&i : G[a]) { if(!visited[i]) s.emplace(i); } } for (auto &&i : Q) { for (auto &&j : G[i]) { if(num[i] < num[j]){ cs[i] += cs[j]; ws[i] += ws[j]; } } } for (auto &&i : Q) { if(S[i] == 'w'){ ans -= (csum-cs[i])*(wsum-ws[i]); for (auto &&j : G[i]) { if(num[i] < num[j]){ ans -= (ll)cs[j]*ws[j]; } } } } cout << ans << "\n"; return 0; }