結果
問題 |
No.439 チワワのなる木
|
ユーザー |
![]() |
提出日時 | 2024-12-20 23:24:11 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 64 ms / 5,000 ms |
コード長 | 1,302 bytes |
コンパイル時間 | 2,000 ms |
コンパイル使用メモリ | 124,628 KB |
実行使用メモリ | 17,664 KB |
最終ジャッジ日時 | 2024-12-20 23:24:15 |
合計ジャッジ時間 | 3,172 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 28 |
ソースコード
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<cassert> #include<utility> #include<queue> #include<stack> #include<vector> #include<map> #include<set> #include<bitset> #define rep(i,l,r) for(int i=(l);i<=(r);i++) #define per(i,r,l) for(int i=(r);i>=(l);i--) #define all(vc) vc.begin(),vc.end() #define sz(vc) ((int)vc.size()) #define pb push_back #define fi first #define se second #define x0 __x00 #define y0 __y00 using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; const int N=1e5+5; string s; int n,w,c,f[N],g[N]; vector<int>G[N]; ll ans; void dfs(int x,int fa){ f[x]+=s[x]=='w'; g[x]+=s[x]=='c'; for(auto to:G[x]){ if(to==fa)continue; dfs(to,x); f[x]+=f[to],g[x]+=g[to]; } if(s[x]=='c')return; for(auto to:G[x]){ if(to==fa)continue; ans+=1ll*g[to]*(w-f[to]-1); } ans+=1ll*(f[x]-1)*(c-g[x]); } int main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); cin>>n>>s; s=' '+s; rep(i,2,n){ int u,v;cin>>u>>v; G[u].pb(v);G[v].pb(u); } rep(i,1,n)w+=s[i]=='w',c+=s[i]=='c'; dfs(1,-1); cout<<ans<<'\n'; return 0; }