結果

問題 No.439 チワワのなる木
ユーザー hirokazu1020hirokazu1020
提出日時 2016-10-29 01:53:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 65 ms / 5,000 ms
コード長 1,379 bytes
コンパイル時間 871 ms
コンパイル使用メモリ 96,276 KB
実行使用メモリ 17,464 KB
最終ジャッジ日時 2023-08-15 22:09:59
合計ジャッジ時間 2,602 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,628 KB
testcase_01 AC 3 ms
5,828 KB
testcase_02 AC 3 ms
5,656 KB
testcase_03 AC 3 ms
5,652 KB
testcase_04 AC 4 ms
5,720 KB
testcase_05 AC 3 ms
5,740 KB
testcase_06 AC 3 ms
5,804 KB
testcase_07 AC 3 ms
5,740 KB
testcase_08 AC 3 ms
5,860 KB
testcase_09 AC 4 ms
5,752 KB
testcase_10 AC 3 ms
5,656 KB
testcase_11 AC 3 ms
5,728 KB
testcase_12 AC 4 ms
5,772 KB
testcase_13 AC 3 ms
5,660 KB
testcase_14 AC 4 ms
5,756 KB
testcase_15 AC 4 ms
5,708 KB
testcase_16 AC 4 ms
5,808 KB
testcase_17 AC 4 ms
5,828 KB
testcase_18 AC 39 ms
8,852 KB
testcase_19 AC 34 ms
8,856 KB
testcase_20 AC 52 ms
9,652 KB
testcase_21 AC 15 ms
6,908 KB
testcase_22 AC 15 ms
6,744 KB
testcase_23 AC 60 ms
10,484 KB
testcase_24 AC 65 ms
16,436 KB
testcase_25 AC 38 ms
9,908 KB
testcase_26 AC 37 ms
9,808 KB
testcase_27 AC 42 ms
17,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<random>
using namespace std;
#define INF (1<<29)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define uniq(v) v.erase(unique(all(v)),v.end())



int n;
string s;
int c[100000], w[100000];
vector<int> edge[100000];
int sw;
long long ans;


void dfs1(int par,int u) {
	c[u] = w[u] = 0;
	if (s[u] == 'c')c[u]++;
	else w[u]++;
	for (int v:edge[u]) {
		if (v == par)continue;
		dfs1(u, v);
		c[u] += c[v];
		w[u] += w[v];
	}
}

void dfs2(int par, int u,int pc,int pw){
	int ew = sw;
	if (s[u] == 'w')ew--;
	for (int v : edge[u]) {
		if (v == par)continue;
		if (s[u] == 'w')ans += (long long)(c[v])*(ew - w[v]);
		dfs2(u, v, pc + c[u] - c[v], pw + w[u] - w[v]);
	}
	if (s[u] == 'w')ans += (long long)(pc)*(ew - pw);
}




int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> s;
	rep(i,n-1) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		edge[a].push_back(b);
		edge[b].push_back(a);
	}
	dfs1(-1, 0);
	sw = count(all(s), 'w');
	dfs2(-1, 0, 0, 0);
	cout << ans << endl;

	return 0;
}
0