結果

問題 No.439 チワワのなる木
ユーザー vjudge1
提出日時 2024-12-22 17:24:55
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 46 ms / 5,000 ms
コード長 2,464 bytes
コンパイル時間 1,793 ms
コンパイル使用メモリ 171,160 KB
実行使用メモリ 18,648 KB
最終ジャッジ日時 2024-12-22 17:24:58
合計ジャッジ時間 3,116 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define int long long
#define MAXN 100005
using namespace std;
 
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
 
int n,cnt1=0,cnt2=0;
string s;
int cc[MAXN],cw[MAXN];
vector<int> e[MAXN];
int res=0;
 
void dfs(int x,int y){
    if(s[x]=='c'){
        cc[x]=1;
    }
    if(s[x]=='w'){
        cw[x]=1;
    }
    for(int v:e[x]){
        if(v!=y){
            dfs(v,x);
            cc[x]+=cc[v];
            cw[x]+=cw[v];
            if(s[x]=='w'){
                res+=1LL*cc[v]*(cnt2-1-cw[v]);
            }
        }
    }
    if(y!=-1 && s[x]=='w') {
        res+=1LL*(cnt1-cc[x])*(cw[x]-1);
    }
}
 
signed main(){
    n=read();
    cin>>s;
    for(char c:s){
        if(c=='c'){
            cnt1++;
        }
        else if(c=='w'){
            cnt2++;
        }
    }
    for(int i=0;i<n-1;i++){
        int x,y;
        x=read();y=read();
        e[x-1].push_back(y-1);
        e[y-1].push_back(x-1);
    }
    dfs(0,-1);
    printf("%lld\n",res);
    return 0;
}
/*??
??????
???????????????????????
????????????????????
???????????5???????????????????????????????????????????????????????????????????????????????????????????????24?????????????????????????????????????????????????????????????????????
????????????????????
???????????????????????????????????????????????????????????????????????????????????????????...?????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...????????????????????????????????
??????????????
????????????
?????????????
?????????????
?????????
??? ? ?? ? ???????
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????
???????????????????????????
?????????????????????
????????????????????
?????????????
????????????????
????????????????????????
?????????????????????????
??????
??????????????
????????????
???????????????????
????????????
??????
?????
???????????
??????????????
?????
?????
?????
??????
???????????????
??????????
??????????????
????????????
????*/
0