結果

問題 No.439 チワワのなる木
ユーザー kzyKTkzyKT
提出日時 2016-10-28 23:39:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 2,147 bytes
コンパイル時間 1,403 ms
コンパイル使用メモリ 150,628 KB
実行使用メモリ 18,532 KB
最終ジャッジ日時 2023-08-15 21:43:49
合計ジャッジ時間 3,274 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,736 KB
testcase_01 AC 3 ms
5,920 KB
testcase_02 AC 3 ms
5,900 KB
testcase_03 AC 4 ms
5,704 KB
testcase_04 AC 3 ms
5,684 KB
testcase_05 AC 3 ms
5,728 KB
testcase_06 AC 4 ms
5,688 KB
testcase_07 AC 3 ms
5,780 KB
testcase_08 AC 3 ms
5,924 KB
testcase_09 AC 3 ms
5,708 KB
testcase_10 AC 3 ms
5,740 KB
testcase_11 AC 4 ms
5,792 KB
testcase_12 AC 4 ms
5,712 KB
testcase_13 AC 4 ms
5,796 KB
testcase_14 AC 3 ms
5,756 KB
testcase_15 AC 4 ms
5,784 KB
testcase_16 AC 4 ms
5,772 KB
testcase_17 AC 4 ms
5,840 KB
testcase_18 AC 37 ms
8,840 KB
testcase_19 AC 33 ms
8,888 KB
testcase_20 AC 50 ms
9,812 KB
testcase_21 AC 14 ms
7,116 KB
testcase_22 AC 14 ms
7,100 KB
testcase_23 AC 52 ms
10,844 KB
testcase_24 AC 55 ms
17,244 KB
testcase_25 AC 37 ms
10,136 KB
testcase_26 AC 38 ms
9,976 KB
testcase_27 AC 39 ms
18,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
template<C T>void pr(T a){cout<<a;ln;}
template<C T,C T2>void pr(T a,T2 b){cout<<a<<' ';pr(b);}
template<C T,C T2,C T3>void pr(T a,T2 b,T3 c){cout<<a<<' ';pr(b,c);}
template<C T,C T2,C T3,C T4>void pr(T a,T2 b,T3 c,T4 d){cout<<a<<' ';pr(b,c,d);}
template<C T>void PR(T a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
typedef pair<int,P> PP;

vector<PP> v[100001];
char c[100001];
ll n,d=0;
P solve(int i,int p) {
  if(v[i].size()==1&&v[i][0].F==p) {
    v[i][0].S=P(n-d-(c[i]=='w'),d-(c[i]=='c'));
    return P(c[i]=='w',c[i]=='c');
  }
  int k=-1;
  P q=P(0,0);
  for(int j=0; j<v[i].size(); j++) {
    if(v[i][j].F!=p) {
      if(v[i][j].S==P(-1,-1)) {
        v[i][j].S=solve(v[i][j].F,i);
        q.F+=v[i][j].S.F;
        q.S+=v[i][j].S.S;
      } else {
        q.F+=v[i][j].S.F;
        q.S+=v[i][j].S.S;
      }
    } else k=j;
  }
  if(k!=-1) {
    v[i][k].second=P(n-d-q.F-(c[i]=='w'),d-q.S-(c[i]=='c'));
  }
  return P(q.F+(c[i]=='w'),q.S+(c[i]=='c'));
}

void Main() {
  string s;
  cin >> n >> s;
  rep(i,n) {
    c[i]=s[i];
    if(s[i]=='c') d++;
  }
  rep(i,n-1) {
    int x,y;
    cin >> x >> y;
    x--,y--;
    v[x].pb(PP(y,P(-1,-1)));
    v[y].pb(PP(x,P(-1,-1)));
  }
  solve(0,-1);
  ll ans=0;
  rep(x,n) {
    if(c[x]=='w') {
      rep(i,v[x].size()) {
        P p=v[x][i].S;
        ans+=p.F*(d-p.S);
      }
    }
  }
  pr(ans);
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}
0