2019ICPC沈阳网络赛-D-Fish eating fruit(树上DP, 换根, 点分治)
链接:
https://nanti.jisuanke.com/t/41403
题意:
State Z is a underwater kingdom of the Atlantic Ocean. This country is amazing. There are nn cities in the country and n-1n−1 undirected underwater roads which connect all cities.
In order to save energy and avoid traffic congestion, the king promulgated a series of traffic regulations:
Residents have to travel on fish!
Residents need to feed the fish before you start your trip!The amount of food you feed the fish should be exactly the total distance of your journey.
What kind of food to feed depends on the total distance of your journey!Total distance is a multiple of three. You should feed the fish with Durian. Total distance modulus 33 equaling 11. It should be Papaya.Total distance modulus 33 equaling 22. It should be Avocado!!!
Sure, fish like to eat these fruits very much.
Today is the first day of the king's decree. Because the residents here are not good at mathematics, they don't know how much fruit they need to give fish to go to other cities. So the king give an order to the energy minister Ynaonlctrm From all cities to all cities directly, which means that he will make n*(n-1)n∗(n−1) trips.
For example, A - (5 mile) - B - (5 mile) - C, he needs to run the fish, starting at A, passing B, finally arriving C (papaya 10 kg), also he needs to start at C and end at A (papaya 10 kg). Indirect passage is useless. "I've passed City B, my dear emperor." "Oh! It's no use! Not directly! People in cities will never know how much the fish need to eat! The fish will surely die!!! You also need to take several trips which start at B or end with B!" The Emperor said.
It's really a tough task. Can you help him figure out how much fruit he needs to prepare for the emperor's mission?
思路:
题解是树上DP, 比赛时候想到DP,和点分治,但是都没怎么学过, 赛后看题解..还要换根..没听过.
换根就是先DFS一遍之后,根据第一遍DFS得到的结果去处理第二遍DFS,第二遍DFS就是将每个点都看成一个根.
本题令Cnt1[i][j]为以root为根,i的子树中距i的距离%3为j的点的数量.Dp1[i][j]则是i的子树中距i为j的距离路径总和.
这两种状态可以用一遍DFS求得, root就是自己设的根.
第二遍DFS就是处理每个点为根的情况.
考虑Cnt2[i][j], 在以root为根的情况下, 非i的子节点到i的距离%3为j的节点个数.
令v为当前节点, u为其父节点
推出式子
Cnt2[v][(j+len)%3] = Cnt1[u][j]-Cnt1[v][((j-len)%3+3)%3]+Cnt2[u][j].
其中因为u的子节点包括了v的子节点, 需要处理掉.就是减掉.在加上父亲非子节点的贡献.
考虑路径和.同理, 减掉多余的贡献.加上父亲节点的非子节点的贡献.具体看代码.
点分治代码留着补
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 2e4+10;
struct Node
{
int to;
int dis;
};
int Cnt1[MAXN][3], Cnt2[MAXN][3];
LL Dp1[MAXN][3], Dp2[MAXN][3];
LL res[3];
vector<Node> G[MAXN];
int n;
void Dfs1(int u, int v)
{
Cnt1[v][0] = 1;
for (int i = 0;i < G[v].size();i++)
{
int to = G[v][i].to;
int len = G[v][i].dis;
if (to == u)
continue;
Dfs1(v, to);
for (int j = 0;j < 3;j++)
{
Cnt1[v][(j+len)%3] += Cnt1[to][j];
Dp1[v][(j+len)%3] = (Dp1[v][(j+len)%3] + Dp1[to][j] + (len*Cnt1[to][j])%MOD)%MOD;
}
}
}
void Dfs2(int u, int v)
{
// cout << u << ' ' << v << endl;
for (int i = 0;i < G[v].size();i++)
{
int to = G[v][i].to;
int len = G[v][i].dis;
if (to == u)
continue;
for (int j = 0;j < 3;j++)
{
Cnt2[to][(j+len)%3] = Cnt1[v][j] - Cnt1[to][((j-len)%3+3)%3] + Cnt2[v][j];
Dp2[to][(j+len)%3] = ((((Dp1[v][j] - Dp1[to][((j-len)%3+3)%3]) - (len*Cnt1[to][((j-len)%3+3)%3]))%MOD+MOD)%MOD
+ Dp2[v][j] + (len*Cnt2[to][(j+len)%3])%MOD)%MOD;
}
Dfs2(v, to);
}
}
int main()
{
while (~scanf("%d", &n))
{
for (int i = 1;i <= n;i++)
G[i].clear();
memset(Cnt1, 0, sizeof(Cnt1));
memset(Cnt2, 0, sizeof(Cnt2));
memset(Dp1, 0, sizeof(Dp1));
memset(Dp2, 0, sizeof(Dp2));
memset(res, 0, sizeof(res));
int l, r, v;
for (int i = 1;i < n;i++)
{
scanf("%d%d%d", &l, &r, &v);
l++, r++;
G[l].push_back(Node{r, v});
G[r].push_back(Node{l, v});
}
Dfs1(0, 1);
Dfs2(0, 1);
for (int i = 1;i <= n;i++)
{
for (int j = 0;j < 3;j++)
res[j] = (res[j] + Dp1[i][j] + Dp2[i][j])%MOD;
}
printf("%lld %lld %lld\n", res[0], res[1], res[2]);
}
return 0;
}
2019ICPC沈阳网络赛-D-Fish eating fruit(树上DP, 换根, 点分治)的更多相关文章
- 2019icpc沈阳网络赛 D Fish eating fruit 树形dp
题意 分别算一个树中所有简单路径长度模3为0,1,2的距离和乘2. 分析 记录两个数组, \(dp[i][k]\)为距i模3为k的子节点到i的距离和 \(f[i][k]\)为距i模3为k的子节点的个数 ...
- 2019 沈阳网络赛 D Fish eating fruit ( 树形DP)
题目传送门 题意:求一颗树中所有点对(a,b)的路径长度,路径长度按照模3之后的值进行分类,最后分别求每一类的和 分析:树形DP \(dp[i][j]\) 表示以 i 为根的子树中,所有子节点到 i ...
- 2019ICPC沈阳网络赛-B-Dudu's maze(缩点)
链接: https://nanti.jisuanke.com/t/41402 题意: To seek candies for Maomao, Dudu comes to a maze. There a ...
- 2019ICPC上海网络赛 A Lightning Routing I 点分树(动态点分治)+线段树
题意 给一颗带边权的树,有两种操作 \(C~e_i~w_i\),将第\(e_i\)条边的边权改为\(w_i\). \(Q~v_i\),询问距\(v_i\)点最远的点的距离. 分析 官方题解做法:动态维 ...
- Fish eating fruit 沈阳网络赛(树形dp)
Fish eating fruit \[ Time Limit: 1000 ms \quad Memory Limit: 262144 kB \] 题意 大体的题意就是给出一棵树,求每一对点之间的距离 ...
- 2018 ICPC 沈阳网络赛
2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...
- 线段树+单调栈+前缀和--2019icpc南昌网络赛I
线段树+单调栈+前缀和--2019icpc南昌网络赛I Alice has a magic array. She suggests that the value of a interval is eq ...
- 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 ...
- 沈阳网络赛 F - 上下界网络流
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
随机推荐
- 外连接的用法 -- 《SQL进阶教程》 jupyter note
import pandas as pd import sqlite3 conn = sqlite3.connect('1-5.db') 用外连接进行行列转换1(行 -> 列): 制作交叉表 怎么 ...
- 【MapReduce】二、MapReduce编程模型
通过前面的实例,可以基本了解MapReduce对于少量输入数据是如何工作的,但是MapReduce主要用于面向大规模数据集的并行计算.所以,还需要重点了解MapReduce的并行编程模型和运行机制 ...
- 轻度折腾nuc8i5beh
最近入手了一台迷你电脑:Intel NUC--Next Unit of Computing,配了1根16GB内存条(2666)和两块固态(m.2+sata),搭载i5-8259U ,Intel Iri ...
- Prometheus 和 Alertmanager实战配置
Prometheus时序数据库 一.Prometheus 1.Prometheus安装 1)源码安装 prometheus安装包最新版本下载地址:https://prometheus.io/downl ...
- AKKA文档2.1(java版)——什么是AKKA?
可扩展的实时事务处理 我们相信编写并发.容错.可扩展的应用相当的困难.盖因大多数时候我们一直在使用错误的工具和错误的抽象等级.AKKA就是为了改变这一切的.我们利用角色模型提升了抽象等级,并且提供了一 ...
- 【Linux-驱动】RTC设备驱动架构
在Linux操作系统中,RTC设备驱动的架构如下图所示: RTC设备驱动涉及的文件:class.c.rtc-dev.c : 建立/dev/rtc0设备,同时注册相应的操作函数.interface.c ...
- GrapeCity Documents for Excel 文档API组件 V2.2 新特性介绍
GrapeCity Documents for Excel 文档API组件 V2.2 正式发布,本次新版本包含诸多重量级产品功能,如:将带有形状的电子表格导出为 PDF.控制分页和电子表格内容.将Ex ...
- 【DP 好题】Kick Start 2019 Round C Catch Some
题目链接 题目大意 在一条数轴上住着 $N$ 条狗和一个动物研究者 Bundle.Bundle 的坐标是 0,狗的坐标都是正整数,可能有多条狗住在同一个位置.每条狗都有一个颜色.Bundle 需要观测 ...
- 基于 Vue.js 2.0 酷炫自适应背景视频登录页面的设计『转』
本文讲述如何实现拥有酷炫背景视频的登录页面,浏览器窗口随意拉伸,背景视频及前景登录组件均能完美适配,背景视频可始终铺满窗口,前景组件始终居中,视频的内容始终得到最大限度的保留,可以得到最好的视觉效果. ...
- ubuntu 18.04 配置notebook远程连接的坑
jupyter-notebook的安装不在此说明 在网上搜了很多方案都不行,好不容易从坑里爬出来 以下为远程连接配置方法 1.生成配置文件 jupyter notebook --generate-co ...