結果

問題 No.439 チワワのなる木
ユーザー mayoko_mayoko_
提出日時 2016-04-28 21:31:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,094 bytes
コンパイル時間 1,868 ms
コンパイル使用メモリ 169,188 KB
実行使用メモリ 14,240 KB
最終ジャッジ日時 2024-04-19 07:34:30
合計ジャッジ時間 8,654 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// とりあえず TLE 解

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll mod = 1e9 + 7;

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

// first: ww second: w
pll dfs(int v, int p) {
    pll ret;
    if (s[v] == 'w') ret.second = 1;
    for (int ch : G[v]) if (ch != p) {
        pll tmp = dfs(ch, v);
        ret.first += tmp.first;
        ret.second += tmp.second;
        if (s[v] == 'w') ret.first += tmp.second;
    }
    return ret;
}

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

    cin >> N;
    cin >> s;
    for (int i = 0; i < N; i++) assert(s[i] == 'c' || s[i] == 'w');

    G.resize(N);
    for (int i = 0; i < N-1; i++) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    // すべての c から木DP
    ll ans = 0;
    for (int i = 0; i < N; i++) {
        if (s[i] == 'c') {
            pll tmp = dfs(i, -1);
            ans += tmp.first;
        }
    }
    cout << ans << endl;
}
0