結果

問題 No.439 チワワのなる木
ユーザー re_re0101re_re0101
提出日時 2021-02-23 03:05:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 5,000 ms
コード長 2,940 bytes
コンパイル時間 4,005 ms
コンパイル使用メモリ 267,984 KB
実行使用メモリ 18,256 KB
最終ジャッジ日時 2023-10-21 20:33:37
合計ジャッジ時間 5,845 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,840 KB
testcase_01 AC 4 ms
7,840 KB
testcase_02 AC 5 ms
7,840 KB
testcase_03 AC 5 ms
7,840 KB
testcase_04 AC 5 ms
7,840 KB
testcase_05 AC 5 ms
7,840 KB
testcase_06 AC 5 ms
7,840 KB
testcase_07 AC 5 ms
7,840 KB
testcase_08 AC 4 ms
7,840 KB
testcase_09 AC 5 ms
7,840 KB
testcase_10 AC 5 ms
7,840 KB
testcase_11 AC 5 ms
7,840 KB
testcase_12 AC 4 ms
7,840 KB
testcase_13 AC 5 ms
7,840 KB
testcase_14 AC 5 ms
7,840 KB
testcase_15 AC 5 ms
7,840 KB
testcase_16 AC 5 ms
7,840 KB
testcase_17 AC 6 ms
7,840 KB
testcase_18 AC 43 ms
11,128 KB
testcase_19 AC 36 ms
11,128 KB
testcase_20 AC 62 ms
12,184 KB
testcase_21 AC 16 ms
9,280 KB
testcase_22 AC 16 ms
9,016 KB
testcase_23 AC 65 ms
12,976 KB
testcase_24 AC 68 ms
17,464 KB
testcase_25 AC 46 ms
12,712 KB
testcase_26 AC 40 ms
12,768 KB
testcase_27 AC 39 ms
18,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define bit(n,k) (((ll)n>>(ll)k)&1) /*nのk bit目*/
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define eb emplace_back
#define endl '\n'
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define PI 3.14159265359
const double eps = 1e-12;
const long long INF= (long long)1e18+20;
typedef double D;      // 座標値の型。doubleかlong doubleを想定
typedef complex<D> Point;  // Point
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl>vvl;
typedef vector<vvl>vvvl;
typedef vector<vvvl>vvvvl;
typedef vector<vvvvl>vvvvvl;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> T;
template<class T> using minpq=priority_queue<T,vector<T>,greater<T>>;
const ll MOD=1000000007LL;
// const ll MOD=998244353LL;
const ll mod=MOD;
string abc="abcdefghijklmnopqrstuvwxyz";
string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vl dx={0,0,1,-1,1,1,-1,-1};
vl dy={1,-1,0,0,-1,1,-1,1};

template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}


using mint=modint998244353;

vvl G(100100);
string s;
vector<P>dp(100100);
P dfs(ll cur=0,ll par=-1){
    ll ww=0,w=0;
    for(ll to:G[cur]){
        if(par==to)continue;
        P p=dfs(to,cur);
        ww+=p.first;
        w+=p.second;
    }
    if(s[cur]=='w'){
        ww+=w;
        w++;
    }
    return dp[cur]={ww,w};
}

vector<ll>dp2(100100);
void dfs2(ll cur=0,ll par=-1){
    if(par!=-1){
        ll w=dp[0].second-dp[cur].second;
        ll ww=dp2[par]-dp[cur].first-(s[par]=='w'?dp[cur].second:0);
        dp2[cur]=dp[cur].first+ww+(s[cur]=='w'?w:0);
        // if(cur==2)cout<<w<<" "<<ww<<" "<<dp2[cur]<<endl;
    }
    for(ll to:G[cur]){
        if(par==to)continue;
        dfs2(to,cur);
    }
    return;
}

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(12);
    /*--------------------------------*/
    
    int n;cin>>n;
    cin>>s;
    vector<int>a(n-1),b(n-1);
    for(int i=0;i<n-1;i++){
        cin>>a[i]>>b[i];a[i]--;b[i]--;
        G[a[i]].pb(b[i]);
        G[b[i]].pb(a[i]);
    }
    dfs();
    dp2[0]=dp[0].first;
    dfs2();
    ll ans=0;
    for(int i=0;i<n;i++){
        if(s[i]=='c')ans+=dp2[i];
    }
    // rep(i,n)cout<<i<<" "<<dp[i].first<<" "<<dp[i].second<<endl;
    // rep(i,n)cout<<i<<" "<<dp2[i]<<endl;
    cout<<ans<<endl;
}
0