結果

問題 No.439 チワワのなる木
ユーザー chocoruskchocorusk
提出日時 2018-12-17 00:04:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 95 ms / 5,000 ms
コード長 1,066 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 99,876 KB
実行使用メモリ 16,956 KB
最終ジャッジ日時 2023-10-25 22:25:04
合計ジャッジ時間 3,440 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,196 KB
testcase_01 AC 2 ms
6,196 KB
testcase_02 AC 2 ms
6,196 KB
testcase_03 AC 2 ms
6,196 KB
testcase_04 AC 2 ms
6,196 KB
testcase_05 AC 2 ms
6,196 KB
testcase_06 AC 2 ms
6,196 KB
testcase_07 AC 2 ms
6,200 KB
testcase_08 AC 3 ms
6,200 KB
testcase_09 AC 3 ms
6,200 KB
testcase_10 AC 3 ms
6,204 KB
testcase_11 AC 2 ms
6,200 KB
testcase_12 AC 3 ms
6,200 KB
testcase_13 AC 3 ms
6,212 KB
testcase_14 AC 3 ms
6,216 KB
testcase_15 AC 3 ms
6,256 KB
testcase_16 AC 4 ms
6,276 KB
testcase_17 AC 4 ms
6,260 KB
testcase_18 AC 65 ms
9,392 KB
testcase_19 AC 57 ms
9,336 KB
testcase_20 AC 84 ms
10,440 KB
testcase_21 AC 24 ms
7,472 KB
testcase_22 AC 23 ms
7,232 KB
testcase_23 AC 92 ms
11,308 KB
testcase_24 AC 95 ms
15,900 KB
testcase_25 AC 71 ms
10,308 KB
testcase_26 AC 66 ms
10,836 KB
testcase_27 AC 78 ms
16,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
ll ctc[100000], ctw[100000];
bool used[100000];
vector<int> g[100000];
int n;
string s;
void dfs(int x){
	used[x]=1;
	if(s[x]=='c') ctc[x]++;
	else ctw[x]++;
	for(auto y:g[x]){
		if(used[y]) continue;
		dfs(y);
		ctc[x]+=ctc[y];
		ctw[x]+=ctw[y];
	}
}
ll ans;
void dfs2(int x){
	used[x]=1;
	ll sw=ctw[0]-1;
	for(auto y:g[x]){
		if(used[y]) continue;
		if(s[x]=='w'){
			ans+=((sw-ctw[y])*ctc[y]);
		}
		dfs2(y);
	}
	if(s[x]=='w') ans+=((ctc[0]-ctc[x])*(ctw[x]-1));
}

int main()
{
	cin>>n; cin>>s;
	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);
	fill(used, used+n, 0);
	dfs2(0);
	cout<<ans<<endl;
	return 0;
}
0