結果

問題 No.439 チワワのなる木
ユーザー furafura
提出日時 2020-06-12 12:11:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 955 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 203,592 KB
実行使用メモリ 18,932 KB
最終ジャッジ日時 2023-09-06 09:03:12
合計ジャッジ時間 5,363 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 2 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 75 ms
17,088 KB
testcase_25 AC 46 ms
9,724 KB
testcase_26 WA -
testcase_27 AC 44 ms
18,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

using graph=vector<vector<int>>;

void add_undirected_edge(graph& G,int u,int v){
	G[u].emplace_back(v);
	G[v].emplace_back(u);
}

graph T;
string s;

int ccnt[100000],wcnt[100000];

lint dfs(int u,int p,int pc,int pw){ // pc, pw : 自身より上の部分木に含まれる c と w の個数
	lint res=0;
	if(s[u]=='c'){
		for(int v:T[u]) if(v!=p) {
			res+=dfs(v,u,pc+1,pw);
			ccnt[u]+=ccnt[v];
			wcnt[u]+=wcnt[v];
		}
		ccnt[u]++;
	}
	else{
		for(int v:T[u]) if(v!=p) {
			res+=dfs(v,u,pc,pw+1);
			res-=lint(ccnt[v])*wcnt[v];
			ccnt[u]+=ccnt[v];
			wcnt[u]+=wcnt[v];
		}
		res+=lint(ccnt[u]+pc)*(wcnt[u]+pw);
		res-=lint(pc)*pw;
		wcnt[u]++;
	}
	return res;
}

int main(){
	int n; cin>>n>>s;
	T.resize(n);
	rep(i,n-1){
		int u,v; scanf("%d%d",&u,&v); u--; v--;
		add_undirected_edge(T,u,v);
	}

	cout<<dfs(0,-1,0,0)<<'\n';

	return 0;
}
0