結果

問題 No.439 チワワのなる木
ユーザー latte0119latte0119
提出日時 2016-10-28 23:00:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 85 ms / 5,000 ms
コード長 1,218 bytes
コンパイル時間 1,526 ms
コンパイル使用メモリ 147,252 KB
実行使用メモリ 18,292 KB
最終ジャッジ日時 2023-08-15 21:28:38
合計ジャッジ時間 3,358 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,752 KB
testcase_01 AC 3 ms
5,768 KB
testcase_02 AC 2 ms
5,704 KB
testcase_03 AC 2 ms
5,764 KB
testcase_04 AC 3 ms
5,888 KB
testcase_05 AC 3 ms
5,844 KB
testcase_06 AC 2 ms
5,764 KB
testcase_07 AC 3 ms
5,708 KB
testcase_08 AC 3 ms
5,756 KB
testcase_09 AC 3 ms
5,856 KB
testcase_10 AC 2 ms
5,756 KB
testcase_11 AC 3 ms
5,712 KB
testcase_12 AC 3 ms
5,708 KB
testcase_13 AC 3 ms
5,728 KB
testcase_14 AC 3 ms
6,032 KB
testcase_15 AC 3 ms
5,848 KB
testcase_16 AC 4 ms
5,800 KB
testcase_17 AC 4 ms
5,836 KB
testcase_18 AC 55 ms
9,580 KB
testcase_19 AC 50 ms
9,484 KB
testcase_20 AC 70 ms
10,868 KB
testcase_21 AC 21 ms
7,516 KB
testcase_22 AC 19 ms
7,128 KB
testcase_23 AC 78 ms
11,760 KB
testcase_24 AC 85 ms
17,484 KB
testcase_25 AC 61 ms
10,784 KB
testcase_26 AC 57 ms
10,988 KB
testcase_27 AC 67 ms
18,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N;
string S;

vint G[100000];

int ans;
int allC,allW;
int C[100000],W[100000];
void dfs(int v,int p){
    if(S[v]=='c')C[v]++;
    else W[v]++;

    for(auto u:G[v]){
        if(u==p)continue;
        dfs(u,v);
        C[v]+=C[u];
        W[v]+=W[u];
    }

    if(S[v]=='c')return;
    for(auto u:G[v]){
        if(u==p)continue;
        ans+=C[u]*(allW-W[u]-1);
    }
    ans+=(allC-C[v])*(W[v]-1);
}

signed main(){
    cin>>N>>S;
    rep(i,N-1){
        int a,b;
        cin>>a>>b;
        a--;b--;
        G[a].pb(b);G[b].pb(a);
    }
    rep(i,N){
        if(S[i]=='c')allC++;
        else allW++;
    }

    dfs(0,-1);
    cout<<ans<<endl;
    return 0;
}
0