結果

問題 No.439 チワワのなる木
ユーザー tossytossy
提出日時 2016-10-29 14:48:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,465 bytes
コンパイル時間 804 ms
コンパイル使用メモリ 100,724 KB
実行使用メモリ 19,912 KB
最終ジャッジ日時 2024-05-03 15:48:38
合計ジャッジ時間 2,099 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
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 52 ms
18,304 KB
testcase_25 AC 40 ms
11,008 KB
testcase_26 WA -
testcase_27 AC 41 ms
19,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<(x)<<endl
#define fi first
#define se second

#define INF 2147483600

vector<vector<int> > vec;
vector<char> label;
vector<long> memoc, memow;
int sumc, sumw;

long dfs(int from, int d){
  long tmpc=0, tmpw=0, ret=0;
  for(auto i : vec[d]) if(i != from){
    ret += dfs(d, i);
  }
  for(auto i : vec[d]) if(i != from){
    tmpc += memoc[i];
    tmpw += memow[i];
  }
  memoc[d] = tmpc + (label[d]=='c');
  memow[d] = tmpw + (label[d]=='w');

  if(label[d]=='w'){
    ret += tmpw*(sumc-tmpc) + tmpc*(sumw-tmpw-1);
  }
  return ret;
}

int main(){
  int n;
  scanf("%d\n", &n);
  vec = vector<vector<int> >(n,vector<int>());
  label = vector<char>(n);
  rep(i,n) scanf("%c", &label[i]);
  rep(i,n-1){
    int a,b;
    scanf("%d %d", &a, &b);
    a--; b--;
    vec[a].pb(b);
    vec[b].pb(a);
  }
  sumc=0; sumw=0;
  rep(i,n){
    if(label[i]=='c') sumc++;
    else sumw++;
  }

  memoc = vector<long>(n, 0);
  memow = vector<long>(n, 0);

  cout<<dfs(-1, 0)<<endl;

  return 0;
}
0