题意:求一棵树上所有路径和模3分别为0 1 2 的权值的和

思路:树形dp 增加一个记录儿子节点满足条件的个数的数组 不要放在一起dp不然答案跟新会有问题

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int N = 2e4+7;
typedef long long ll;
const ll mod = 1e9+7 ;
using namespace std;
struct edge{
ll v,next,w;
};
edge e[N<<1];
ll head[N];
ll tot=0;
void add(ll u,ll v,ll w){
e[++tot]=edge{v,head[u],w};
head[u]=tot;
}
ll cnt[N][3],fcnt[N][3];
ll dp[N][3],fdp[N][3];
void dfs(ll u,ll fa){
//cout<<u<<endl;
cnt[u][0]=1;
for(int i=head[u];i;i=e[i].next){
ll v=e[i].v;
ll w=e[i].w;
if(v==fa) continue;
dfs(v,u);
for(int j=0;j<3;j++){
cnt[u][(j+w)%3]=(cnt[u][(j+w)%3]+cnt[v][j]);
dp[u][(j+w)%3]=(dp[u][(j+w)%3]+dp[v][j]+w*cnt[v][j]%mod)%mod;
//cout<<u<<" "<<dp[v][j]<<" "<<w*cnt[v][j]<<" "<<dp[u][(j+w)%3]<<endl;
}
}
}
void dfss(ll u,ll fa){
for(int i=head[u];i;i=e[i].next){
ll v=e[i].v;
ll w=e[i].w;
if(v==fa) continue;
for(int j=0;j<3;j++){
fcnt[v][(j+w)%3]=(fcnt[u][j]+cnt[u][j]-cnt[v][((j-w)%3+3)%3]);
fdp[v][(j+w)%3]=(fdp[u][j]+(dp[u][j]-dp[v][((j-w)%3+3)%3]-w*cnt[v][((j-w)%3+3)%3]%mod+mod)%mod
+w*fcnt[v][(j+w)%3]%mod)%mod;
}
dfss(v,u);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
while(cin>>n){
tot=0;
memset(head,0,sizeof(head));
memset(dp,0,sizeof(dp));
memset(fdp,0,sizeof(fdp));
memset(cnt,0,sizeof(cnt));
memset(fcnt,0,sizeof(fcnt));
for(int i=1;i<n;i++){
ll u,v,w;
cin>>u>>v>>w;
u++; v++;
add(u,v,w); add(v,u,w);
}
dfs(1,0);
dfss(1,0);
//cout<<fcnt[2][0]<<endl;
ll ans[3]={0};
for(int i=1;i<=n;i++){
for(int j=0;j<3;j++){
//cout<<i<<" "<<j<<" "<<cnt[i][j]<<" "<<fcnt[i][j]<<endl;
ans[j]=(ans[j]+dp[i][j]+fdp[i][j])%mod;
//cout<<i<<" "<<j<<" "<<dp[i][j]<<" "<<fdp[i][j]<<endl;
}
}
cout<<ans[0]%mod<<" "<<ans[1]%mod<<" "<<ans[2]%mod<<endl;
}
return 0;
}
/*
5
0 1 1
0 2 2
0 3 1
1 4 3
*/

The Preliminary Contest for ICPC Asia Shenyang 2019 D. Fish eating fruit(树形dp)的更多相关文章

  1. The Preliminary Contest for ICPC Asia Shenyang 2019

    传送门 B. Dudu's maze 题意: 是什么鬼东西???我读题可以读半小时QAQ 给出一张无向图,一个人在里面收集糖果,每个点都有一个糖果,特殊点除外.当他第一次进入特殊点时,会随机往一条边走 ...

  2. The Preliminary Contest for ICPC Asia Shenyang 2019 F. Honk's pool

    题目链接:https://nanti.jisuanke.com/t/41406 思路:如果k的天数足够大,那么所有水池一定会趋于两种情况: ① 所有水池都是一样的水位,即平均水位 ② 最高水位的水池和 ...

  3. The Preliminary Contest for ICPC Asia Shenyang 2019 H. Texas hold'em Poker

    题目链接:https://nanti.jisuanke.com/t/41408 题目意思很简单,就是个模拟过程. #include <iostream> #include <cstr ...

  4. The Preliminary Contest for ICPC Asia Shenyang 2019 C. Dawn-K's water

    题目:https://nanti.jisuanke.com/t/41401思路:完全背包 #include<bits/stdc++.h> using namespace std; int ...

  5. The Preliminary Contest for ICPC Asia Shenyang 2019 H

    H. Texas hold'em Poker 思路:根据每个牌型分等级,然后排序按照等级优先,最大值次之,次大值,最后比较剩下值的和. #include<bits/stdc++.h> us ...

  6. The Preliminary Contest for ICPC Asia Shenyang 2019 C Dawn-K's water (完全背包)

    完全背包为什么要取到M,可以取到2*M嘛,这题需要整取,对于不能整取的背包容量,dp[k]=INF,以及dp[j-water[i].weight]=INF时,dp[j]也不需要更新.如果不整取的话,后 ...

  7. The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力)

    The Preliminary Contest for ICPC Asia Shanghai 2019 C Triple(FFT+暴力) 传送门:https://nanti.jisuanke.com/ ...

  8. The Preliminary Contest for ICPC Asia Nanjing 2019/2019南京网络赛——题解

    (施工中……已更新DF) 比赛传送门:https://www.jisuanke.com/contest/3004 D. Robots(期望dp) 题意 给一个DAG,保证入度为$0$的点只有$1$,出 ...

  9. 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)

    query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...

随机推荐

  1. day119:MoFang:宠物的状态改动&宠物粮道具的使用&宠物死亡处理

    目录 1.宠物的状态改动 2.宠物粮道具的使用 3.宠物死亡处理 1.宠物的状态改动 1.在setting表中为每个宠物配置生命周期时间 因为宠物有多个,每个宠物会有不同的初始生命的饥饿时间,所以我们 ...

  2. 万万没想到,面试中,连 ClassLoader类加载器 也能问出这么多问题…..

    1.类加载过程 类加载时机 「加载」 将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在内存上创建一个java.lang.Class对象用来封装类在方法区内的数据 ...

  3. Kubernetes官方java客户端之七:patch操作

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  4. 【Java基础】多线程

    多线程 基本概念 程序(program)是为完成特定任务.用某种语言编写的一组指令的集合.即指一段静态的代码,静态对象. 进程(process)是程序的一次执行过程,或是正在运行的一个程序.是一个动态 ...

  5. js的函数-function

    function函数 function的英文是[功能],[数] 函数:职责:盛大的集会的意思 在js里,就是函数的意思.在Java里叫做方法. 定义函数 function fun(参数){ //函数体 ...

  6. Linux简单Shell脚本监控MySQL、Apache Web和磁盘空间

    Linux简单Shell脚本监控MySQL.Apache Web和磁盘空间 1. 目的或任务 当MySQL数据库.Apache Web服务器停止运行时,重新启动运行,并发送邮件通知: 当服务器磁盘的空 ...

  7. linux之平均负载(学习笔记非原创)

    什么是平均负载 [root@111 ~]# uptime 11:03:33 up 149 days, 17:34, 1 user, load average: 0.08, 0.05, 0.01 最后三 ...

  8. 【MySQL】Last_SQL_Errno: 1594Relay log read failure: Could not parse relay log event entry...问题总结处理

    备库报错: Last_SQL_Errno: 1594 Last_SQL_Error: Relay log read failure: Could not parse relay log event e ...

  9. 【windows】快捷键

    Ctrl+字母键 1.Ctrl+A:全选 2.Ctrl+C:复制选择的项目 3.Ctrl+E:选择搜索框 4.Ctrl+F:选择搜索框 5.Ctrl+N:创建新的项目 6.Ctrl+W:关闭当前窗口 ...

  10. 类转json的基类实现

    类转json的基类实现 项目地址 github地址 实现原理 使用反射获取类的属性名和属性内容.具体原理可以自己查一下资料 对一个类调用getClass().getDeclaredFields()可以 ...