bzoj 2152 聪聪可可

简单的树dp

记录到该节点的路径权值x,x(mod3)=v的点的个数,然后就可以轻松的转移了

#include<bits/stdc++.h>
using namespace std;

const int maxn = 21234;
vector<pair<int,int> >edge[maxn];

void init(int n){
    for(int i=0;i<=n;i++){
        edge[i].resize(0);
    }
}

void Link(int st,int ed,int v){
    v %= 3;
    edge[st].push_back(make_pair(ed,v));
    edge[ed].push_back(make_pair(st,v));
}

int dp[maxn][3];
int cnt[maxn];

void dfs(int st,int fa){
    cnt[st] = 1;
    dp[st][0] = 1;
    for(vector<pair<int,int> >::iterator it = edge[st].begin();it!=edge[st].end();it++){
        int x = it->first;
        int v = it->second;
        if(x!=fa){
            dfs(x,st);
            cnt[st] += cnt[x];
            for(int i=0;i<3;i++) cnt[st] += dp[st][i]*dp[x][(6-i-v)%3];
            for(int i=0;i<3;i++) dp[st][(i+v)%3] += dp[x][i];
        }
    }
}

int main(){
    int n;
    while(~scanf("%d",&n)){
        init(n);
        int x,y,v;
        for(int i=1;i<n;i++){
            scanf("%d %d %d",&x,&y,&v);
            Link(x,y,v);
        }
        memset(dp,0,sizeof(dp));
        dfs(1,0);
        int ans = cnt[1] * 2 - n;
        int d = __gcd(ans,n*n);
        printf("%d/%d\n",ans/d,n*n/d);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值