結果
問題 | No.439 チワワのなる木 |
ユーザー |
![]() |
提出日時 | 2018-12-04 21:24:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 184 ms / 5,000 ms |
コード長 | 1,178 bytes |
コンパイル時間 | 2,253 ms |
コンパイル使用メモリ | 201,388 KB |
最終ジャッジ日時 | 2025-01-06 18:12:13 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 28 |
ソースコード
#include<bits/stdc++.h> using namespace std; using Int = long long; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} //INSERT ABOVE HERE signed main(){ Int n; string s; cin>>n>>s; vector<vector<Int> > G(n); for(Int i=1;i<n;i++){ Int x,y; cin>>x>>y; x--;y--; G[x].emplace_back(y); G[y].emplace_back(x); } vector<Int> w(n,0),ww(n,0); { function<void(Int, Int)> dfs= [&](Int v,Int p){ w[v]=s[v]=='w'; for(Int u:G[v]){ if(u==p) continue; dfs(u,v); w[v]+=w[u]; ww[v]+=ww[u]; if(s[v]=='w') ww[v]+=w[u]; } }; dfs(0,-1); } Int ans=0,wa=0; for(Int i=0;i<n;i++) wa+=s[i]=='w'; function<void(Int, Int, Int)> dfs= [&](Int v,Int p,Int x){ //cout<<v<<":"<<x<<" "<<ww[v]<<endl; if(s[v]=='c') ans+=x+ww[v]; for(Int u:G[v]){ if(u==p) continue; if(s[v]=='c') dfs(u,v,x+(ww[v]-ww[u])); else dfs(u,v,x+(ww[v]-(w[u]+ww[u]))+(wa-w[v])); } }; dfs(0,-1,0); cout<<ans<<endl; return 0; }