結果

問題 No.439 チワワのなる木
ユーザー nanophoto12nanophoto12
提出日時 2016-11-05 22:31:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 118 ms / 5,000 ms
コード長 2,091 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 94,068 KB
実行使用メモリ 44,356 KB
最終ジャッジ日時 2023-08-16 13:56:13
合計ジャッジ時間 3,311 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
34,664 KB
testcase_01 AC 14 ms
34,624 KB
testcase_02 AC 14 ms
34,612 KB
testcase_03 AC 14 ms
34,608 KB
testcase_04 AC 15 ms
34,660 KB
testcase_05 AC 14 ms
34,604 KB
testcase_06 AC 14 ms
34,632 KB
testcase_07 AC 14 ms
34,660 KB
testcase_08 AC 14 ms
34,628 KB
testcase_09 AC 15 ms
34,672 KB
testcase_10 AC 14 ms
34,804 KB
testcase_11 AC 14 ms
34,644 KB
testcase_12 AC 14 ms
34,664 KB
testcase_13 AC 14 ms
34,620 KB
testcase_14 AC 15 ms
34,772 KB
testcase_15 AC 14 ms
34,632 KB
testcase_16 AC 16 ms
34,816 KB
testcase_17 AC 15 ms
34,672 KB
testcase_18 AC 74 ms
36,992 KB
testcase_19 AC 70 ms
36,944 KB
testcase_20 AC 96 ms
37,660 KB
testcase_21 AC 36 ms
35,660 KB
testcase_22 AC 33 ms
35,420 KB
testcase_23 AC 118 ms
38,484 KB
testcase_24 AC 109 ms
43,008 KB
testcase_25 AC 84 ms
38,104 KB
testcase_26 AC 82 ms
38,112 KB
testcase_27 AC 84 ms
44,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <vector>
#include <map>
#include <functional>
#include <queue>
#include <iostream>
#include <string.h>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstdint>
#include <climits>
#include <unordered_set>
#include <sstream>

using namespace std;

#define ll long long int
#define ti4 tuple<int,int,int,int>
#define pii pair<ll,ll>
#define REP(x,n) for(int x = 0;x < n;x++)

struct UnionFind {
    vector<ll> data;
    UnionFind(ll size) : data(size, -1) { }
    bool unionSet(ll x, ll y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(ll x, ll y) {
        return root(x) == root(y);
    }
    ll root(ll x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    ll size(ll x) {
        return -data[root(x)];
    }
};

#define MAX_COUNT 1000001

string ss;

vector<int> g[MAX_COUNT];

int cc[MAX_COUNT];
int cw[MAX_COUNT];

ll ret;
ll tc;
ll tw;

void dfs(int current, int prev)
{
    if(ss[current] == 'w') cw[current]=1;
    else if(ss[current] == 'c') cc[current]=1;
    
    for(auto e : g[current])
    {
        if(e == prev)
        {
            continue;
        }
        dfs(e, current);
        cw[current] += cw[e];
        cc[current] += cc[e];
        if(ss[current] == 'w')
        {
            ret += cc[e] * (tw-1-cw[e]);
        }
    }
    if(prev >= 0 && ss[current] == 'w')
    {
        ret += (tc-cc[current]) * (cw[current]-1);
    }
}

int main()
{
    ret = 0;
    tc = tw = 0;
    fill(cc, cc+MAX_COUNT,0);
    fill(cw, cw+MAX_COUNT,0);
    int n;
    cin >> n;
    cin >> ss;
    for(auto r : ss)
    {
        if(r == 'w')
        {
            tw++;
        }
        else
        {
            tc++;
        }
    }
    for(int i = 0;i < n-1;i++)
    {
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(0,-1);
    cout << ret << endl;
    return 0;
}
0