zoukankan      html  css  js  c++  java
  • BZOJ2152 聪聪可可

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

    本文作者:ljh2000
    作者博客:http://www.cnblogs.com/ljh2000-jump/
    转载请注明出处,侵权必究,保留最终解释权!

    题目链接:BZOJ2152

    正解:树形$DP$

    解题报告:

      直接自下往上树形$DP$一遍,$f[x][0、1、2]$表示子树内模$3$余数为$0$、$1$、$2$的总数,每次合并统计答案即可。

      听说有人这道题写树分治==

    //It is made by ljh2000
    //有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <ctime>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <string>
    #include <complex>
    #include <bitset>
    using namespace std;
    typedef long long LL;
    typedef long double LB;
    typedef complex<double> C;
    const double pi = acos(-1);
    const int MAXN = 200011;
    const int MAXM = 400011;
    int n,ecnt,first[MAXN],to[MAXM],next[MAXM],w[MAXM];
    LL f[MAXN][3],ans,g[3],ans2;
    inline LL gcd(LL x,LL y){ if(y==0) return x; return gcd(y,x%y); }
    inline int getint(){
        int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
        if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
    }
    
    inline void link(int x,int y,int z){
    	next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z;
    	next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x; w[ecnt]=z;
    }
    
    inline void dfs(int x,int fa){
    	f[x][0]++;
    	for(int i=first[x];i;i=next[i]) {
    		int v=to[i]; if(v==fa) continue;
    		dfs(v,x);
    		for(int j=0;j<3;j++) g[(w[i]+j)%3]=f[v][j];
    		for(int j=0;j<3;j++) ans+=(LL)g[j]*f[x][(3-j)%3];
    		for(int j=0;j<3;j++) f[x][j]+=g[j];
    	}
    }
    
    inline void work(){
    	n=getint(); int x,y,z; for(int i=1;i<n;i++) { x=getint(); y=getint(); z=getint(); link(x,y,z); }
    	dfs(1,0); ans<<=1; ans+=n; ans2=(LL)n*n;
    	LL G=gcd(ans,ans2);//!!!
    	ans/=G; ans2/=G;
    	printf("%lld/%lld",ans,ans2);
    }
    
    int main()
    {
        work();
        return 0;
    }
    //有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
    

      

  • 相关阅读:
    Java中的日期(Calendar、Date)
    java上传、下载、删除ftp文件
    JAVA中使用FTPClient实现文件上传下载
    使用JSch实现SFTP文件传输
    linux 如何显示一个文件的某几行(中间几行)
    java常用流处理工具StreamTool 常见的InputStream流转字符串, 转字节数组等等
    String与InputStream互转的几种方法
    day 13
    day 12
    day11 大纲
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/6550683.html
Copyright © 2011-2022 走看看