結果

問題 No.439 チワワのなる木
ユーザー minatominato
提出日時 2020-04-09 10:12:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 5,000 ms
コード長 1,571 bytes
コンパイル時間 2,140 ms
コンパイル使用メモリ 176,772 KB
実行使用メモリ 17,824 KB
最終ジャッジ日時 2023-09-28 21:58:19
合計ジャッジ時間 3,687 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,264 KB
testcase_01 AC 3 ms
6,072 KB
testcase_02 AC 4 ms
6,096 KB
testcase_03 AC 3 ms
6,076 KB
testcase_04 AC 3 ms
6,264 KB
testcase_05 AC 3 ms
6,260 KB
testcase_06 AC 3 ms
6,064 KB
testcase_07 AC 3 ms
6,344 KB
testcase_08 AC 3 ms
6,076 KB
testcase_09 AC 3 ms
6,104 KB
testcase_10 AC 3 ms
6,136 KB
testcase_11 AC 3 ms
6,132 KB
testcase_12 AC 3 ms
6,036 KB
testcase_13 AC 3 ms
6,088 KB
testcase_14 AC 3 ms
6,156 KB
testcase_15 AC 3 ms
6,340 KB
testcase_16 AC 4 ms
6,296 KB
testcase_17 AC 4 ms
6,184 KB
testcase_18 AC 41 ms
9,984 KB
testcase_19 AC 33 ms
9,944 KB
testcase_20 AC 60 ms
11,036 KB
testcase_21 AC 14 ms
7,828 KB
testcase_22 AC 15 ms
7,492 KB
testcase_23 AC 70 ms
11,976 KB
testcase_24 AC 89 ms
15,700 KB
testcase_25 AC 42 ms
11,096 KB
testcase_26 AC 42 ms
11,392 KB
testcase_27 AC 40 ms
17,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define ln '\n'
constexpr long long MOD = 1000000007LL;
//constexpr long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull; 
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true;} return false; }
template<class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true;} return false; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int N;
string S;
vector<int> G[101010];
ll C[101010],W[101010];
ll dp[101010];
void dfs1(int v, int pv) {
    if (S[v]=='c') C[v]++;
    else W[v]++;
    for (auto nv : G[v]) {
        if (nv==pv) continue;
        dfs1(nv,v);
        C[v] += C[nv];
        W[v] += W[nv];
    }
}

void dfs2(int v, int pv) {
    for (auto nv:G[v]) {
        if (nv==pv) {
            if (S[v]=='w') dp[v] += (W[v]-1)*(C[0]-C[v]);    
            continue;
        }
        if (S[v]=='w') dp[v] += (W[0] - W[nv] - 1)*C[nv];
        dfs2(nv,v);
    }
}
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> N >> S;
    rep(i,N-1) {
        int u,v; cin >> u >> v;
        --u; --v;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    dfs1(0,-1);
    dfs2(0,-1);
    ll ans = 0;
    rep(i,N) ans += dp[i];
    cout << ans << ln;
}
0