結果

問題 No.439 チワワのなる木
ユーザー ゆにぽけゆにぽけ
提出日時 2024-01-03 18:40:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 54 ms / 5,000 ms
コード長 1,348 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 135,020 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2024-01-03 18:40:25
合計ジャッジ時間 3,889 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 2 ms
6,548 KB
testcase_15 AC 2 ms
6,548 KB
testcase_16 AC 2 ms
6,548 KB
testcase_17 AC 2 ms
6,548 KB
testcase_18 AC 36 ms
7,936 KB
testcase_19 AC 31 ms
7,808 KB
testcase_20 AC 46 ms
9,344 KB
testcase_21 AC 13 ms
6,548 KB
testcase_22 AC 12 ms
6,548 KB
testcase_23 AC 51 ms
10,240 KB
testcase_24 AC 54 ms
14,336 KB
testcase_25 AC 40 ms
10,112 KB
testcase_26 AC 38 ms
10,128 KB
testcase_27 AC 38 ms
15,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <array>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;
void solve()
{
	int N;
	string S;
	cin >> N >> S;
	vector<vector<int>> G(N);
	for(int i = 1;i < N;i++)
	{
		int u,v;
		cin >> u >> v;
		u--; v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	vector<int> C(N),W(N);
	auto dfs = [&](auto dfs,int u,int p) -> void
	{
		if(S[u] == 'c') C[u]++;
		else W[u]++;
		for(int v : G[u]) if(v != p)
		{
			dfs(dfs,v,u);
			C[u] += C[v];
			W[u] += W[v];
		}
	};
	dfs(dfs,0,-1);
	long long ans = 0;
	for(int u = 0;u < N;u++) if(S[u] == 'w')
	{
		long long cur = (long long)C[0] * (W[0] - 1);
		for(int v : G[u])
		{
			int c = 0,w = 0;
			if(C[v] > C[u] || W[v] > W[u])
			{
				c = C[0] - C[u];
				w = W[0] - W[u];
			}
			else
			{
				c = C[v];
				w = W[v];
			}
			cur -= (long long)c * w;
		}
		ans += cur;
	}
	cout << ans << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) solve();
}

0