题意

分别算一个树中所有简单路径长度模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. pb菜单详解和MDI

    菜单条-MenuBar.菜单项-MenuItem.级联菜单(子菜单)-SubMenu 菜单项(MenuItem)是菜单中最基本的元素,只要有文字内容的就是菜单项.菜单条(MenuBar)是菜单中级别最 ...

  2. 植物大战僵尸:寻找召唤僵尸关键CALL

    实验目标:通过遍历寻找召唤僵尸的CALL,通过调用CALL出现自定义的僵尸,加速僵尸的出现. 僵尸CALL的遍历技巧: 我们可以通过僵尸出现在屏幕中的个数来遍历寻找僵尸出现的CALL 首先打开CE-& ...

  3. ngixn二级域名

    每个人的配置不一样,我说说我的 安装完nginx后,找到nginx配置文件/usr/local/nginx/conf/nginx.conf nginx代理apche(作为一级域名) 默认一级域名(ds ...

  4. JArray

    [{ "A001033": "", ", ", ", ", ", ", ", " ...

  5. Java多线程(十一):线程组

    线程组 线程组可以批量管理线程和线程组对象. 一级关联 例子如下,建立一级关联. public class MyThread43 implements Runnable{ public void ru ...

  6. 进程程序替换(自主实现shell)

    进程替换 替换进程所运行的程序 流程:将另一段代码加载到内存中,通过页表将原来进程的映射关系重新建立到新的程序在内存中的地址,相当于替换了进程所运行程序以及所要处理的数据 (替换了代码段,重新初始化数 ...

  7. jquery model 框设定

    https://www.bootcdn.cn/   国内网址引用 js function searchItemInfo(conditionNo,lotCD,itemKey) { var conditi ...

  8. 【PR笔记】一、打造希区柯克变焦效果

    1. 导入素材,“链接选择项”关闭,删除音频 2. 添加效果--视频效果--扭曲--视频稳定器, 然后程序帮我们自动稳定 3.视频首尾添加关键帧,首帧缩放200%  尾帧不变, 使视频前后的主体大小差 ...

  9. PropertiesUtils(普遍做法)

    public class PropertiesUtil{ private static Properties properties; static{ InputStream in = null; tr ...

  10. .net工作流引擎ccflow集成并增加自定义功能

    一.为什么需要自定义扩展 1.第三方类库已满足大部分需求,剩下的根据具体业务需求抽象成公共功能进行扩展 2.第三方呈现的web页面与原类库耦合度较高,希望在原页面上扩展而不影响原来的功能 3.在完全不 ...