結果

問題 No.439 チワワのなる木
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-25 19:24:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,426 bytes
コンパイル時間 1,883 ms
コンパイル使用メモリ 171,320 KB
実行使用メモリ 14,236 KB
最終ジャッジ日時 2024-04-19 07:23:55
合計ジャッジ時間 9,413 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;

const ll mod = 1e9 + 7;

int N;
string s;
vector<vector<int>> G;

vector<ll> dfs(int now, int pre) {
    vector<ll> ret(3, 0);  // w, ww, cwwの個数

    for (auto nxt : G[now]) {
        if (nxt == pre) continue;
        auto child = dfs(nxt, now);
        rep(i, 3) ret[i] += child[i];
    }

    if (s[now] == 'c') {
        // 自分がcなら、子のwwの数だけcwwが増える
        ret[2] += ret[1];
    } else {
        // 自分がwなら、子のwの数だけwwが増える
        ret[1] += ret[0];
        // wは1増える
        ret[0]++;
    }

    rep(i, 3) ret[i] %= mod;

    return ret;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    cin >> N;
    cin >> s;

    G.resize(N);
    rep(i, N - 1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }


    // すべての c から木DP
    ll ans = 0;
    rep(i, N) {
        if (s[i] == 'c') {
            auto ret = dfs(i, -1);
            ans += ret[2];
            ans %= mod;
        }
    }

    cout << ans << endl;

}
0