結果

問題 No.439 チワワのなる木
ユーザー koprickykopricky
提出日時 2017-08-08 16:55:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 54 ms / 5,000 ms
コード長 2,024 bytes
コンパイル時間 1,231 ms
コンパイル使用メモリ 147,340 KB
実行使用メモリ 17,504 KB
最終ジャッジ日時 2023-08-02 05:09:52
合計ジャッジ時間 3,576 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,752 KB
testcase_01 AC 3 ms
5,708 KB
testcase_02 AC 3 ms
5,708 KB
testcase_03 AC 3 ms
5,704 KB
testcase_04 AC 3 ms
5,764 KB
testcase_05 AC 4 ms
5,708 KB
testcase_06 AC 3 ms
5,772 KB
testcase_07 AC 4 ms
5,920 KB
testcase_08 AC 3 ms
5,692 KB
testcase_09 AC 3 ms
5,720 KB
testcase_10 AC 3 ms
5,708 KB
testcase_11 AC 3 ms
5,756 KB
testcase_12 AC 3 ms
5,756 KB
testcase_13 AC 3 ms
5,728 KB
testcase_14 AC 3 ms
5,724 KB
testcase_15 AC 4 ms
5,880 KB
testcase_16 AC 5 ms
5,860 KB
testcase_17 AC 4 ms
5,816 KB
testcase_18 AC 34 ms
8,836 KB
testcase_19 AC 31 ms
8,704 KB
testcase_20 AC 46 ms
9,488 KB
testcase_21 AC 13 ms
6,984 KB
testcase_22 AC 14 ms
6,800 KB
testcase_23 AC 48 ms
10,660 KB
testcase_24 AC 54 ms
16,196 KB
testcase_25 AC 36 ms
9,992 KB
testcase_26 AC 36 ms
9,824 KB
testcase_27 AC 38 ms
17,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout <<" "<<kbrni;cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

string s;
int alc,alw;
vector<int> G[MAX_N];
int memc[MAX_N],memw[MAX_N];
ll ans;

void dfs(int u,int p)
{
    if(G[u].size() == 1 && G[u][0] == p){
        if(s[u] == 'c'){
            memc[u] = 1;
            memw[u] = 0;
        }else{
            memc[u] = 0;
            memw[u] = 1;
        }
        return;
    }
    if(memc[u] >= 0 && memw[u] >= 0){
        return;
    }
    int smc = 0,smw = 0;
    rep(i,G[u].size()){
        if(G[u][i] != p){
            dfs(G[u][i],u);
            smc += memc[G[u][i]];
            smw += memw[G[u][i]];
        }
    }
    if(s[u] == 'c'){
        memc[u] = smc+1;
        memw[u] = smw;
    }else{
        memc[u] = smc;
        memw[u] = smw+1;
        int remc = alc - memc[u];
        int remw = alw - memw[u];
        rep(i,G[u].size()){
            if(G[u][i] != p){
                ans += (ll)memc[G[u][i]] * (alw-memw[G[u][i]]-1);
            }else{
                ans += (ll)remc*(alw-remw-1);
            }
        }
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    cin >> s;
    rep(i,n){
        if(s[i] == 'c'){
            alc++;
        }else{
            alw++;
        }
    }
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        G[a-1].pb(b-1);
        G[b-1].pb(a-1);
    }
    rep(i,n){
        memc[i] = memw[i] = -1;
    }
    dfs(0,-1);
    cout << ans << endl;
    return 0;
}
0