Fish eating fruit 沈阳网络赛(树形dp)
Fish eating fruit
\]
题意
大体的题意就是给出一棵树,求每一对点之间的距离,然后把该距离存在距离 \(\mod 3\) 的位置,输出总和。
思路
令两个 \(dp\) 数组和两个辅助 \(dp\) 的数组。
\(dp1[i][j]\) 表示从 \(i\) 为起点往下到各个点距离 \(\mod 3\) 后为 \(j\) 的距离总和。
\(cnt1[i][j]\) 表示以 \(i\) 为起点往下到各个点距离 \(\mod 3\) 后为 \(j\) 的节点个数。
\(dp2[i][j]\) 表示从 \(i\) 起点往上一步后到各个点距离 \(\mod 3\) 后为 \(j\) 的距离总和。
\(cnt2[i][j]\) 表示以 \(i\) 为起点往上一步后到各个点距离 \(\mod 3\) 后为 \(j\) 的节点个数。
对于两个 \(dp\) 分别跑一遍 \(dfs\)
对于 \(dp1\) 比较好处理,直接往下 \(dfs\)
以 \(u\) 开始的答案等于从 \(v\) 开始的答案加上这一条边 \(w\) 的贡献,可以得到
cnt1[u][(j+w)\%3] = \sum cnt1[v][j]
\]
对于 \(dp2\) 会比较麻烦,需要用 \(fa\) 节点向上的贡献在加上 \(fa\) 节点往下的贡献在减去 \(fa\) 节点往 \(u\) 走的贡献。这些节点就是 \(u\) 往上走一步后可以走到的所有节点。这样算出真实的节点数和距离总和,然后 \(u\) 才能开始转移。
设 \(faw\) 为从 \(u\) 到 \(fa\) 的路径长度
计算真实的节点数:
c[(j+faw)\%3] -= cnt1[u][j]
\]
计算真实的距离总和:
d[(j+faw)\%3] -= dp1[u][0]+cnt1[u][j]*faw
\]
则最后的 \(dp2\) 就可以利用 \(d\) 和 \(c\) 得到了
cnt2[u][(j+faw)\%3] = c[j]
\]
/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : Mon 16 Sep 2019 08:55:33 PM CST
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
vector< pii > vv[maxn];
ll cnt1[maxn][3], cnt2[maxn][3];
ll dp1[maxn][3], dp2[maxn][3];
void dfs1(int u, int fa) {
cnt1[u][0] = 1;
for(auto i : vv[u]) {
int v = i.fi, w = i.se;
if(v == fa) continue;
dfs1(v, u);
dp1[u][(0+w)%3] += (cnt1[v][0]*w%mod+dp1[v][0])%mod;
dp1[u][(1+w)%3] += (cnt1[v][1]*w%mod+dp1[v][1])%mod;
dp1[u][(2+w)%3] += (cnt1[v][2]*w%mod+dp1[v][2])%mod;
for(int j=0; j<3; j++) dp1[u][j] %= mod;
cnt1[u][(0+w)%3] += cnt1[v][0];
cnt1[u][(1+w)%3] += cnt1[v][1];
cnt1[u][(2+w)%3] += cnt1[v][2];
}
}
void dfs2(int u, int fa) {
if(u!=1) {
int faw;
for(auto i : vv[u]) {
if(i.fi == fa) {
faw = i.se;
break;
}
}
int c[3] = { 0 };
c[0] = cnt2[fa][0]+cnt1[fa][0];
c[1] = cnt2[fa][1]+cnt1[fa][1];
c[2] = cnt2[fa][2]+cnt1[fa][2];
c[(0+faw)%3] -= cnt1[u][0];
c[(1+faw)%3] -= cnt1[u][1];
c[(2+faw)%3] -= cnt1[u][2];
ll d[3] = { 0 };
d[0] = (dp2[fa][0]+dp1[fa][0])%mod;
d[1] = (dp2[fa][1]+dp1[fa][1])%mod;
d[2] = (dp2[fa][2]+dp1[fa][2])%mod;
d[(0+faw)%3] = ((d[(0+faw)%3] - (cnt1[u][0]*faw%mod+dp1[u][0])%mod+mod)%mod+mod)%mod;
d[(1+faw)%3] = ((d[(1+faw)%3] - (cnt1[u][1]*faw%mod+dp1[u][1])%mod+mod)%mod+mod)%mod;
d[(2+faw)%3] = ((d[(2+faw)%3] - (cnt1[u][2]*faw%mod+dp1[u][2])%mod+mod)%mod+mod)%mod;
dp2[u][(0+faw)%3] = (c[0]*faw%mod+d[0])%mod;
dp2[u][(1+faw)%3] = (c[1]*faw%mod+d[1])%mod;
dp2[u][(2+faw)%3] = (c[2]*faw%mod+d[2])%mod;
cnt2[u][(0+faw)%3] += c[0];
cnt2[u][(1+faw)%3] += c[1];
cnt2[u][(2+faw)%3] += c[2];
}
for(auto i : vv[u]) {
int v = i.fi, w = i.se;
if(v == fa) continue;
dfs2(v, u);
}
}
int main() {
// freopen("in", "r", stdin);
while(~scanf("%d", &n)) {
for(int i=1; i<=n; i++) {
vv[i].clear();
}
mes(dp1, 0), mes(dp2, 0);
mes(cnt1, 0), mes(cnt2, 0);
for(int i=1, u, v, w; i<n; i++) {
scanf("%d%d%d", &u, &v, &w);
u++, v++;
vv[u].pb(make_pair(v, w));
vv[v].pb(make_pair(u, w));
}
dfs1(1, 0);
dfs2(1, 0);
// for(int i=1; i<=n; i++) {
// for(int j=0; j<3; j++) {
// printf("dp1[%d][%d] = %lld, cnt1[%d][%d] = %lld\n", i, j, dp1[i][j], i, j, cnt1[i][j]);
// }
// }
// cout << "-----------------" << endl;
// for(int i=1; i<=n; i++) {
// for(int j=0; j<3; j++) {
// printf("dp2[%d][%d] = %lld, cnt2[%d][%d] = %lld\n", i, j, dp2[i][j], i, j, cnt2[i][j]);
// }
// }
ll ans0, ans1, ans2;
ans0 = ans1 = ans2 = 0;
for(int i=1; i<=n; i++) {
ans0 = (ans0+dp1[i][0]+dp2[i][0])%mod;
ans1 = (ans1+dp1[i][1]+dp2[i][1])%mod;
ans2 = (ans2+dp1[i][2]+dp2[i][2])%mod;
}
printf("%lld %lld %lld\n", ans0, ans1, ans2);
}
return 0;
}
Fish eating fruit 沈阳网络赛(树形dp)的更多相关文章
- HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...
- 2019沈阳网赛树形dp
https://nanti.jisuanke.com/t/41403 2019沈阳网络赛D题 树形dp.一棵树,求任意两个点的距离之和.u-v和v-u算两次.两点之间的距离分为三类,模3等于0,1,2 ...
- hdu 4274 2012长春赛区网络赛 树形dp ***
设定每个节点的上限和下限,之后向上更新,判断是否出现矛盾 #include<cstdio> #include<iostream> #include<algorithm&g ...
- 2018 ICPC 沈阳网络赛
2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...
- 2019 沈阳网络赛 D Fish eating fruit ( 树形DP)
题目传送门 题意:求一颗树中所有点对(a,b)的路径长度,路径长度按照模3之后的值进行分类,最后分别求每一类的和 分析:树形DP \(dp[i][j]\) 表示以 i 为根的子树中,所有子节点到 i ...
- 2019 沈阳网络赛 Fish eating fruit
这题看了三个月,终于过了,第一次看的时候没学树形DP,想用点分治但是不会 后来学了二次扫描,就有点想法了.... 这东西也真就玄学了吧... #include<iostream> #inc ...
- The Preliminary Contest for ICPC Asia Shenyang 2019 D. Fish eating fruit(树形dp)
题意:求一棵树上所有路径和模3分别为0 1 2 的权值的和 思路:树形dp 增加一个记录儿子节点满足条件的个数的数组 不要放在一起dp不然答案跟新会有问题 #include <bits/stdc ...
- 5.21 省选模拟赛 luogu P4297 [NOI2006]网络收费 树形dp
LINK:网络收费 还是自己没脑子. 早上思考的时候 发现树形dp不可做 然后放弃治疗了. 没有合理的转换问题的模型是我整个人最大的败笔. 暴力也值得一提 爆搜之后可以写成FFT的形式的计算贡献的方法 ...
- 沈阳网络赛 F - 上下界网络流
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
随机推荐
- 用欧拉计划学习Rust编程(第13~16题)
最近想学习Libra数字货币的MOVE语言,发现它是用Rust编写的,所以先补一下Rust的基础知识.学习了一段时间,发现Rust的学习曲线非常陡峭,不过仍有快速入门的办法. 学习任何一项技能最怕没有 ...
- 基于 Docker 实现 DevOps 的一些探索
DevOps 介绍 DevOps(Deveplopment 和 Operations 的简称),中译为开发运维一体化,可定义为是一种过程.方法.文化.运动或实践,主要是为了通过一条高度自动化的流水线来 ...
- 创建Visual Studio 2019离线安装包
可以在不同的网络环境和不同的计算机上在线安装微软Visual Studio 2019.微软提供的在线安装工具(Visual Studio web installer)可以让用户在线下载最新版本Visu ...
- 论文阅读: Direct Monocular Odometry Using Points and Lines
Direct Monocular Odometry Using Points and Lines Abstract 大多数VO都用点: 特征匹配 / 基于像素intensity的直接法关联. 我们做了 ...
- ioc与bean管理
IOC称之为控制反转,简单来说就是将对象的创建的权利和对象的声明周期的管理过程交给Spring框架来处理,在这个开发过程中不再需要关注对象的创建和生命周期的管理,而是在需要的时由Spring框架提供, ...
- .NET Standard和.NET Core是什么关系(转载)
.NET Standard vs .NET Core 问: I have read about the difference between .NET Standard and .NET Core, ...
- Kafka分区分配策略-RangeAssignor、RoundRobinAssignor、StickyAssignor
引言按照Kafka默认的消费逻辑设定,一个分区只能被同一个消费组(ConsumerGroup)内的一个消费者消费.假设目前某消费组内只有一个消费者C0,订阅了一个topic,这个topic包含7个分区 ...
- ScheduledThreadPoolExecutor使用指南
ScheduledThreadPoolExecutor是Timer的多线程实现版本,JDK官方推荐使用.ScheduledThreadPoolExecutor用于替代Timer.是接口Schedule ...
- C#给企业微信中的成员发送消息
先去企业微信门户网站获得密钥和应用ID 创建一个静态工具类 using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using ...
- .net core - 配置管理 - json文件配置
Json 文件配置 public class Startup { public Startup(IHostingEnvironment env) { var builder = new Configu ...