結果

問題 No.439 チワワのなる木
ユーザー mayoko_mayoko_
提出日時 2016-04-26 08:36:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,072 bytes
コンパイル時間 1,478 ms
コンパイル使用メモリ 169,124 KB
実行使用メモリ 14,108 KB
最終ジャッジ日時 2024-04-15 04:33:00
合計ジャッジ時間 8,325 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
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 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> pll;

const ll mod = 1e9 + 7;

int N;
string s;
vector<vector<int>> G;

int dfs(int v, int p) {
    int ret = (s[v] == 'w');
    for (int ch : G[v]) if (ch != p) {
        ret += dfs(ch, v);
    }
    return ret;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    cin >> N;
    cin >> s;

    G.resize(N);
    rep(i, N - 1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    // すべての c から木DP
    ll ans = 0;
    rep(i, N) {
        if (s[i] == 'c') {
            for (int ch : G[i]) {
                ll p = dfs(ch, i);
                ans += p*(p-1)/2;
            }
        }
    }
    cout << ans << endl;
}
0