結果
| 問題 |
No.439 チワワのなる木
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-02-23 16:41:10 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 107 ms / 5,000 ms |
| コード長 | 696 bytes |
| コンパイル時間 | 690 ms |
| コンパイル使用メモリ | 71,424 KB |
| 実行使用メモリ | 19,840 KB |
| 最終ジャッジ日時 | 2024-10-10 03:22:24 |
| 合計ジャッジ時間 | 2,847 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
コンパイルメッセージ
main.cpp:42:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
42 | main()
| ^~~~
ソースコード
#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;
}