結果

問題 No.439 チワワのなる木
ユーザー mamekinmamekin
提出日時 2016-10-28 23:44:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 97 ms / 5,000 ms
コード長 1,930 bytes
コンパイル時間 1,303 ms
コンパイル使用メモリ 112,928 KB
実行使用メモリ 10,832 KB
最終ジャッジ日時 2023-08-15 21:45:13
合計ジャッジ時間 3,125 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 64 ms
8,316 KB
testcase_19 AC 58 ms
8,176 KB
testcase_20 AC 82 ms
9,912 KB
testcase_21 AC 23 ms
5,316 KB
testcase_22 AC 21 ms
5,032 KB
testcase_23 AC 89 ms
10,432 KB
testcase_24 AC 97 ms
10,256 KB
testcase_25 AC 72 ms
10,684 KB
testcase_26 AC 68 ms
10,832 KB
testcase_27 AC 72 ms
10,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

int main()
{
    int n;
    string s;
    cin >> n >> s;
    vector<vector<int> > edges(n);
    for(int i=0; i<n-1; ++i){
        int a, b;
        cin >> a >> b;
        -- a;
        -- b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }

    queue<pair<int, int> > q;
    stack<pair<int, int> > stk;
    q.push(make_pair(0, -1));
    while(!q.empty()){
        pair<int, int> p = q.front();
        q.pop();
        stk.push(p);

        int curr = p.first;
        int prev = p.second;
        for(int next : edges[curr]){
            if(next != prev)
                q.push(make_pair(next, curr));
        }
    }

    int cSum = count(s.begin(), s.end(), 'c');
    int wSum = n - cSum;
    vector<int> cCnt(n, 0), wCnt(n, 0);
    long long ans = 0;
    while(!stk.empty()){
        pair<int, int> p = stk.top();
        stk.pop();

        int curr = p.first;
        int parent = p.second;
        for(int child : edges[curr]){
            if(child != parent){
                cCnt[curr] += cCnt[child];
                wCnt[curr] += wCnt[child];
            }
        }
        if(s[curr] == 'c'){
            ++ cCnt[curr];
            continue;
        }
        
        ++ wCnt[curr];
        ans += (wCnt[curr] - 1LL) * (cSum - cCnt[curr]);
        for(int child : edges[curr]){
            if(child != parent){
                ans += cCnt[child] * (wSum - wCnt[child] - 1LL);
            }
        }
    }
    cout << ans << endl;

    return 0;
}
0