链接:

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;
}

最新文章

  1. CSS 问题集锦
  2. 尽量少用if else
  3. k Sum | &amp; ||
  4. android post请求
  5. nodeschool.io 9
  6. tomcat 192.168.1.110?不烦吗?
  7. centos7启动时出现“无法应用原保存的显示器配置”
  8. DBHelper 类(网上收集)
  9. laravel artisan 命令工具
  10. rsyslog管理分布式日志
  11. awk之随机函数rand()和srand() (转)
  12. 凡事预则立(Beta)
  13. 第三十二节,使用谷歌Object Detection API进行目标检测、训练新的模型(使用VOC 2012数据集)
  14. 雾霾天出行,如何精确避开“雷区”?2016 SODA数据侠十强
  15. 查找IDEA 项目中的依赖包存放在.m2位置
  16. SpringMVC---applicationContext.xml
  17. 继承之super关键字的使用
  18. 安装nginx和添加ssl证书
  19. django1.10.3下admin后台管理老是显示object
  20. Python自动化开发 -进程、线程和协程(二)

热门文章

  1. Linux C/C++基础——文件(上)
  2. Win10黑色白色主题切换
  3. 【DSP开发】回马枪要你命 德州仪器发布最强ARM芯片Keystone II
  4. 天勤考研数据结构笔记—栈的C语言实现
  5. ZooKeeper原理及介绍
  6. 设计模式:状态模式(Status)
  7. CVE-2017-17558漏洞学习
  8. O004、启动第一个KVM虚机
  9. Flask-migrate基本使用方法
  10. 01 Linux常用基本命令(一)