結果

問題 No.439 チワワのなる木
ユーザー kotatsugamekotatsugame
提出日時 2020-02-23 16:41:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 142 ms / 5,000 ms
コード長 696 bytes
コンパイル時間 693 ms
コンパイル使用メモリ 71,556 KB
実行使用メモリ 20,096 KB
最終ジャッジ日時 2024-04-18 10:10:01
合計ジャッジ時間 3,076 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,656 KB
testcase_01 AC 3 ms
6,656 KB
testcase_02 AC 4 ms
6,656 KB
testcase_03 AC 3 ms
6,784 KB
testcase_04 AC 3 ms
6,784 KB
testcase_05 AC 3 ms
6,656 KB
testcase_06 AC 3 ms
6,784 KB
testcase_07 AC 3 ms
6,656 KB
testcase_08 AC 2 ms
6,528 KB
testcase_09 AC 3 ms
6,656 KB
testcase_10 AC 3 ms
6,528 KB
testcase_11 AC 4 ms
6,656 KB
testcase_12 AC 3 ms
6,656 KB
testcase_13 AC 3 ms
6,784 KB
testcase_14 AC 3 ms
6,656 KB
testcase_15 AC 4 ms
6,656 KB
testcase_16 AC 5 ms
6,912 KB
testcase_17 AC 6 ms
6,912 KB
testcase_18 AC 78 ms
9,544 KB
testcase_19 AC 59 ms
9,588 KB
testcase_20 AC 119 ms
10,448 KB
testcase_21 AC 26 ms
7,900 KB
testcase_22 AC 24 ms
7,700 KB
testcase_23 AC 103 ms
11,520 KB
testcase_24 AC 142 ms
18,432 KB
testcase_25 AC 84 ms
10,728 KB
testcase_26 AC 82 ms
10,716 KB
testcase_27 AC 84 ms
20,096 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:42:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   42 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;
int N;
string s;
vector<int>G[1<<17];
int cc[1<<17],cw[1<<17];
void dfs1(int u,int p)
{
	if(s[u]=='c')cc[u]++;
	else cw[u]++;
	for(int v:G[u])
	{
		if(v!=p)
		{
			dfs1(v,u);
			cc[u]+=cc[v];
			cw[u]+=cw[v];
		}
	}
}
long ans=0;
void dfs2(int u,int p,int pc,int pw)
{
	long C=pc+cc[u],W=pw+cw[u];
	if(s[u]=='w')
	{
		ans+=(long)cc[u]*pw;
	}
	for(int v:G[u])
	{
		if(v!=p)
		{
			dfs2(v,u,C-cc[v],W-cw[v]);
			if(s[u]=='w')
			{
				ans+=(C-cc[v])*cw[v];
			}
		}
	}
}
main()
{
	cin>>N>>s;
	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);
	}
	dfs1(0,-1);
	dfs2(0,-1,0,0);
	cout<<ans<<endl;
}
0