結果

問題 No.439 チワワのなる木
ユーザー parukiparuki
提出日時 2016-10-29 00:26:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,388 bytes
コンパイル時間 1,400 ms
コンパイル使用メモリ 170,432 KB
実行使用メモリ 18,984 KB
最終ジャッジ日時 2024-05-03 08:27:28
合計ジャッジ時間 2,698 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,088 KB
testcase_01 AC 3 ms
8,900 KB
testcase_02 AC 4 ms
8,960 KB
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 AC 45 ms
17,608 KB
testcase_25 AC 36 ms
12,980 KB
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

int N;
char S[100010];
vi G[100010], vertices;
ll dp[100010][4], ans = 0;

void dfs(int u, int pre) {
    vertices.push_back(u);
    each(v, G[u])if(v != pre) {
        dfs(v, u);
    }
}

int main(){
    cin >> N;
    scanf("%s", S);
    rep(i, N - 1) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    rep(i, N)if(sz(G[i]) == 1) {
        dfs(i, -1);
        break;
    }

    rep(ITER, 2) {
        MEM(dp, 0);
        dp[0][0] = 1;
        rep(i, N)rep(j, 3) {
            int v = vertices[i];
            ll val = dp[i][j];
            if(S[v] == 'c' && j == 0)dp[i + 1][1] += val;
            if(S[v] == 'w' && j == 1 || j == 2)dp[i + 1][j + 1] += val;
            dp[i + 1][j] += dp[i][j];
        }
        ans += dp[N][3];
        reverse(all(vertices));
    }
    cout << ans << endl;
}
0