题意

分别算一个树中所有简单路径长度模3为0,1,2的距离和乘2。

分析

记录两个数组,

\(dp[i][k]\)为距i模3为k的子节点到i的距离和

\(f[i][k]\)为距i模3为k的子节点的个数

\(ans[k]\)为所有简单路径长度模3为k的距离和

\(v\)是\(u\)的儿子,\(c\)是u到v的边长度,\(0<i,j<3,k=(j-c\%3+3)\%3\)

  • \(dp[u][(i+c\%3)\%3]+=dp[v][i]+f[v][i]*c\)

  • \(f[u][(i+c\%3)\%3]+=f[v][i]\)

  • \(ans[(i+j+c\%3)\%3]+=f[v][i]*(d[u][j]-d[v][k]-f[v][k]*c)\),算u的子节点跨越u的答案

  • \(ans[i]+=dp[u][i]\)

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=1e5+10;
int n;
typedef pair<int,int>pii;
vector<pii>g[maxn];
ll d[maxn][3],f[maxn][3],ans[3];
void dfs(int u,int fa){
f[u][0]=1;
for(pii x:g[u]){
if(x.fi==fa) continue;
int v=x.fi;ll c=x.se;
dfs(x.fi,u);
for(int i=0;i<3;i++){
(d[u][(i+c%3)%3]+=(d[v][i]+f[v][i]*c%mod)%mod)%=mod;
(f[u][(i+c%3)%3]+=f[v][i])%=mod;
}
}
}
void dfs1(int u,int fa){
ans[0]=(ans[0]+d[u][0])%mod;
ans[1]=(ans[1]+d[u][1])%mod;
ans[2]=(ans[2]+d[u][2])%mod;
for(pii x:g[u]){
if(x.fi==fa) continue;
int v=x.fi;ll c=x.se;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int k=(j-c%3+3)%3;
(ans[(i+j+c%3)%3]+=f[v][i]*(d[u][j]-d[v][k]-f[v][k]*c%mod)%mod)%=mod;
}
}
dfs1(x.fi,u);
}
}
int main(){
//ios::sync_with_stdio(false);
//freopen("in","r",stdin);
while(~scanf("%d",&n)){
for(int i=1,a,b,c;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
++a;++b;
g[a].pb(pii(b,c));
g[b].pb(pii(a,c));
}
ans[0]=ans[1]=ans[2]=0;
dfs(1,0);dfs1(1,0);
printf("%lld %lld %lld\n",ans[0]*2%mod,ans[1]*2%mod,ans[2]*2%mod);
for(int i=1;i<=n;i++) g[i].clear(),d[i][0]=d[i][1]=d[i][2]=f[i][0]=f[i][1]=f[i][2]=0;
}
return 0;
}

2019icpc沈阳网络赛 D Fish eating fruit 树形dp的更多相关文章

  1. 2019 沈阳网络赛 D Fish eating fruit ( 树形DP)

    题目传送门 题意:求一颗树中所有点对(a,b)的路径长度,路径长度按照模3之后的值进行分类,最后分别求每一类的和 分析:树形DP \(dp[i][j]\) 表示以 i 为根的子树中,所有子节点到 i ...

  2. 2019ICPC沈阳网络赛-D-Fish eating fruit(树上DP, 换根, 点分治)

    链接: https://nanti.jisuanke.com/t/41403 题意: State Z is a underwater kingdom of the Atlantic Ocean. Th ...

  3. 2019ICPC沈阳网络赛-B-Dudu's maze(缩点)

    链接: https://nanti.jisuanke.com/t/41402 题意: To seek candies for Maomao, Dudu comes to a maze. There a ...

  4. hdu6446 网络赛 Tree and Permutation(树形dp求任意两点距离之和)题解

    题意:有一棵n个点的树,点之间用无向边相连.现把这棵树对应一个序列,这个序列任意两点的距离为这两点在树上的距离,显然,这样的序列有n!个,加入这是第i个序列,那么这个序列所提供的贡献值为:第一个点到其 ...

  5. Fish eating fruit 沈阳网络赛(树形dp)

    Fish eating fruit \[ Time Limit: 1000 ms \quad Memory Limit: 262144 kB \] 题意 大体的题意就是给出一棵树,求每一对点之间的距离 ...

  6. 2018 ICPC 沈阳网络赛

    2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...

  7. 线段树+单调栈+前缀和--2019icpc南昌网络赛I

    线段树+单调栈+前缀和--2019icpc南昌网络赛I Alice has a magic array. She suggests that the value of a interval is eq ...

  8. 2019ICPC南京网络赛A题 The beautiful values of the palace(三维偏序)

    2019ICPC南京网络赛A题 The beautiful values of the palace https://nanti.jisuanke.com/t/41298 Here is a squa ...

  9. 沈阳网络赛 F - 上下界网络流

    "Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...

随机推荐

  1. vue开发环境配置跨域,一步到位

    本文要实现的是:使用vue-cli搭建的项目在开发时配置跨域,上线后不做任何任何修改,接口也可以访问,前端跨域解决方案 production:产品 生产环境 development:开发 开发环境 1 ...

  2. cygwin gcc 编译windowsAPI 报错的一个解决方案

    一开始按照linux的习惯去编译一个使用了windowsAPI的程序 结果提示: $ i686-pc-cygwin-g++ screen_catch.cscreen_catch.c: In funct ...

  3. RabbitMQ安装&简单使用

    .什么是RabbitMQ.详见 http://www.rabbitmq.com/. 作用就是提高系统的并发性,将一些不需要及时响应客户端且占用较多资源的操作,放入队列,再由另外一个线程,去异步处理这些 ...

  4. MySQL SELECT语法(四)UNION语法详解

    源自MySQL 5.7 官方手册:13.2.9.3 UNION Syntax 一.UNION语法 UNION用于将多个SELECT语句的结果合并到一个结果集中. SELECT ... UNION [A ...

  5. 待解决问题 jscore 与 node.js jsbridge

    jscore 与 node.js  jsbridge https://juejin.im/post/5b395eb96fb9a00e556123ef

  6. GoLang语言环境搭建及idea集成开发(超详细)

    一.所需安装包(windows) 1. https://golang.org/dl/  下载 MSI installer.不会翻墙的自己找国内下载,双击运行,按照提示安装即可.环境变量自动配置 2.i ...

  7. oracle_多表查询02

    多表查询 select * from BONUS; select * from DEPT; select * from EMP; select * from SALGRADE; BONUS表 ENAM ...

  8. spring——aop详细总结1

    AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补 ...

  9. Typeof() 和 GetType()区别

    1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...

  10. virtual和override

    偶然间看到的题,借此记录. class Program { static void Main(string[] args) { D d = new D(); //第一个D是申明类,第二个D是实例类 A ...