結果
| 問題 |
No.439 チワワのなる木
|
| ユーザー |
vjudge1
|
| 提出日時 | 2024-12-20 23:16:17 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,301 bytes |
| コンパイル時間 | 1,764 ms |
| コンパイル使用メモリ | 125,180 KB |
| 実行使用メモリ | 17,536 KB |
| 最終ジャッジ日時 | 2024-12-20 23:16:29 |
| 合計ジャッジ時間 | 3,405 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 11 WA * 17 |
ソースコード
#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[x]*(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;
}
vjudge1