結果

問題 No.439 チワワのなる木
ユーザー tnakao0123tnakao0123
提出日時 2016-11-01 21:45:06
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,916 bytes
コンパイル時間 1,715 ms
コンパイル使用メモリ 90,972 KB
実行使用メモリ 15,288 KB
最終ジャッジ日時 2023-08-16 12:29:28
合計ジャッジ時間 3,168 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,796 KB
testcase_01 AC 3 ms
5,904 KB
testcase_02 AC 3 ms
5,792 KB
testcase_03 WA -
testcase_04 AC 3 ms
5,796 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
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 108 ms
14,320 KB
testcase_25 AC 76 ms
10,488 KB
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 439.cc: No.439 チワワのなる木 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;

/* global variables */

int n, tcn, twn;
string s;
vi nbrs[MAX_N];
int cis[MAX_N], wis[MAX_N], ccs[MAX_N], wcs[MAX_N];

/* subroutines */

void rec(int u, int p) {
  vi &nbru = nbrs[u];
  for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
    int &v = *vit;
    if (v == p) continue;

    rec(v, u);
    ccs[u] += cis[v] + ccs[v];
    wcs[u] += wis[v] + wcs[v];
  }
}

/* main */

int main() {
  cin >> n >> s;

  for (int i = 0; i < n; i++)
    (s[i] == 'c') ? (cis[i] = 1, tcn++) : (wis[i] = 1, twn++);
  //printf("tcn=%d, twn=%d\n", tcn, twn);

  for (int i = 0; i < n - 1; i++) {
    int ai, bi;
    cin >> ai >> bi;
    ai--, bi--;
    nbrs[ai].push_back(bi);
    nbrs[bi].push_back(ai);
  }

  rec(0, -1);
  //for (int i = 0; i < n; i++)
  //printf("ccs[%d]=%d, wcs[%d]=%d\n", i, ccs[i], i, wcs[i]);

  queue<pii> q;
  q.push(pii(0, -1));

  ll ans = 0;
  
  while (! q.empty()) {
    pii u = q.front(); q.pop();
    int &ui = u.first, &up = u.second;

    int con = tcn - cis[ui], won = twn - wis[ui];

    if (wis[ui]) ans += (ll)(con - ccs[ui]) * wcs[ui];
    
    vi &nbru = nbrs[ui];
    for (vi::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
      int &vi = *vit;
      if (vi == up) continue;

      if (wis[ui]) ans += (ll)ccs[vi] * (won - wcs[vi]);
      q.push(pii(vi, ui));
    }
  }

  printf("%lld\n", ans);
  return 0;
}
0