一棵树,如果有序点对(x,y)之间路径的长度取模于3==0,那么ans0便加上这个长度;
如果取模于3==1,那么ans1便加上这个长度;
如果取模于3==2,那么ans2便加上这个长度;
让你求ans0,ans1,ans2;
输入格式:
第一行包括一个整数n,表示一共有n个点
下面n-1行,每一行分别输入三个整数a,b,v,代表从a到b有一条长度为v的路径,输入保证不出现环和重边
输出格式:
输出包含三个整数分别为三个答案里存的路径长度之和模1e9+7;
样例:
5
0 1 2
0 2 3
0 3 7
0 4 6
54 60 30
显然,这是一个树形DP,只要记录这个点的子树到这个点的距离的模数就好了;
注意状态转移时的边界;
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#define p 1000000007
#define inc(i,a,b) for(register int i=a;i<=b;i++)
#define dec(i,a,b) for(register int i=a;i>=b;i--)
using namespace std;
template<class nT>
inline void read(nT& x)
{
char c; while(c=getchar(),!isdigit(c));
x=c^48; while(c=getchar(),isdigit(c)) x=x*10+c-48;
}
int head[2000010],cnt;
class littlestar{
public:
int to;
int nxt;
int w;
void add(int u,int v,int gg){
to=v;
nxt=head[u];
w=gg;
head[u]=cnt;
}
}star[2000010];
int g[1000010][4];
void dfs(int u,int fa)
{
g[u][0]=1;
for(int i=head[u];i;i=star[i].nxt){
int v=star[i].to;
if(v==fa) continue;
dfs(v,u);
inc(j,0,2) g[u][(j+star[i].w%3)%3]+=g[v][j];
}
}
long long ans[5],f[1000010][4];
long long tmp[5],tot[5];
void dp(int u,int fa)
{
for(int i=head[u];i;i=star[i].nxt){
int v=star[i].to;
if(v==fa) continue;
dp(v,u);
tmp[0]=tmp[1]=tmp[2]=0;
tot[0]=tot[1]=tot[2]=0;
inc(j,0,2){
tmp[(j+star[i].w)%3]=(tmp[(j+star[i].w)%3]+f[v][j])%p;
tmp[(j+star[i].w)%3]=(tmp[(j+star[i].w)%3]+g[v][j]*star[i].w%p)%p;
tot[(j+star[i].w)%3]=(tot[(j+star[i].w)%3]+g[v][j])%p;
}
inc(j,0,2){
inc(k,0,2){
ans[(j+k)%3]=(ans[(j+k)%3]+tmp[j]*(g[u][k]-tot[k]))%p;
}
}
inc(j,0,2){
f[u][j]=(f[u][j]+tmp[j])%p;
}
}
}
int main()
{
int n; read(n);
inc(i,1,n-1){
int u,v,w; read(u); read(v); read(w);
++u; ++v;
star[++cnt].add(u,v,w);
star[++cnt].add(v,u,w);
}
dfs(1,0);
dp(1,0);
cout<<ans[0]*2%p<<" "<<ans[1]*2%p<<" "<<ans[2]*2%p;
}
原文:https://www.cnblogs.com/kamimxr/p/11722474.html