結果

問題 No.439 チワワのなる木
ユーザー minatominato
提出日時 2020-04-09 10:11:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,619 bytes
コンパイル時間 2,054 ms
コンパイル使用メモリ 178,100 KB
実行使用メモリ 17,576 KB
最終ジャッジ日時 2023-09-28 21:57:42
合計ジャッジ時間 4,591 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
    rep(i,N) cout << C[i] << " " << W[i] << ln;
    ll ans = 0;
    rep(i,N) ans += dp[i];
    cout << ans << ln;
}
0