結果

問題 No.439 チワワのなる木
ユーザー parukiparuki
提出日時 2016-10-29 01:10:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,062 bytes
コンパイル時間 1,855 ms
コンパイル使用メモリ 177,588 KB
実行使用メモリ 23,664 KB
最終ジャッジ日時 2023-08-15 22:06:34
合計ジャッジ時間 3,431 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 1 ms
4,380 KB
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 78 ms
21,960 KB
testcase_25 AC 44 ms
13,108 KB
testcase_26 WA -
testcase_27 AC 54 ms
23,664 KB
権限があれば一括ダウンロードができます

ソースコード

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;

vector<vector<int> > makeTree(const vector<int> &u, const vector<int> &v) {
    int V = (int)u.size() + 1;
    vector<vector<int> > tree(V), graph(V);
    for(int i = 0; i < V - 1; ++i) {
        graph[u[i]].push_back(v[i]);
        graph[v[i]].push_back(u[i]);
    }
    queue<pair<int, int> > Q;
    Q.push(make_pair(0, -1));
    while(!Q.empty()) {
        int cur, pre;
        tie(cur, pre) = Q.front();
        Q.pop();
        for(auto &to : graph[cur]) if(to != pre) {
            tree[cur].push_back(to);
            Q.push(make_pair(to, cur));
        }
    }
    return tree;
}

const int MA = 100010;
int N;
char S[100010];
vector<vi> G;
ll dp[100010][4], ww[MA], wc[MA], nw[MA], nc[MA];

ll dfs(int u) {
    if(S[u] == 'w')nw[u]++;
    if(S[u] == 'c')nc[u]++;

    ll res = 0;
    each(v, G[u]) {
        res += dfs(v);
        nw[u] += nw[v];
        nc[u] += nc[v];
        ww[u] += ww[v];
        wc[u] += wc[v];
        if(S[u] == 'w') {
            ww[u] += nw[v];
            wc[u] += nc[v];
        }
    }

    each(v, G[u]) {
        if(S[u]=='c') res += ww[v];
        else if(S[u] == 'w')res += wc[v];
    }

    ll sumW = nw[u] - !!(S[u] == 'w');
    each(v, G[u]) {
        ll numW = sumW - nw[v], numC = nc[v];
        res += numW*numC;
    }
    return res;
}

int main(){
    cin >> N;
    scanf("%s", S);
    vi A(N - 1), B(N - 1);
    rep(i, N - 1) {
        int a, b;
        scanf("%d%d", &a, &b);
        A[i] = a - 1;
        B[i] = b - 1;
    }
    G = makeTree(A, B);
    ll ans = dfs(0);
    cout << ans << endl;
}
0