题目:https://www.luogu.org/problemnew/show/P1351

树形DP,别忘了子树之间的情况(拐一下距离为2)。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const maxn=2e5+,mod=;
int n,hd[maxn],ct,to[maxn<<],nxt[maxn<<];
ll w[maxn],ans,sum,mxx[maxn],s[maxn];
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return ret*f;
}
void add(int x,int y){to[++ct]=y; nxt[ct]=hd[x]; hd[x]=ct;}
void dfs(int x,int f)
{
ll ns=,nm=;//不是全局变量!
for(int i=hd[x],u;i;i=nxt[i])
{
if((u=to[i])==f)continue;
dfs(u,x); sum=(sum+s[u]*w[x])%mod;
ans=max(ans,mxx[u]*w[x]);
s[x]+=w[u]; mxx[x]=max(mxx[x],w[u]);
sum=(sum+ns*w[u])%mod; ns+=w[u];
ans=max(ans,nm*w[u]); nm=max(nm,w[u]);
}
}
int main()
{
n=rd();
for(int i=,x,y;i<n;i++)
{
x=rd(); y=rd();
add(x,y); add(y,x);
}
for(int i=;i<=n;i++)w[i]=rd();
dfs(,);
printf("%lld %lld\n",ans,(sum*)%mod);
return ;
}

最新文章

  1. 使用mac终端生成RSA私钥和公钥文件
  2. Oracle安装注意点与工具使用简说
  3. 集合的概念 Stack和Queue Dictionary ArrayList和List&lt;T&gt;方法及用法
  4. phpcms标签使用 —— 系统常量
  5. Android 日常开发总结的技术经验 60 条
  6. java 21-13 合并
  7. 欧拉工程第55题:Lychrel numbers
  8. pdo 连接数据库 报错 could not find driver 解决方法
  9. Webstorm 配置与使用 Less
  10. ViewPager的用法
  11. bootchart--检测linux启动性能的软件
  12. php 抽象类abstract
  13. 在后台业务管理系统中使用Autofac实现微信接口的处理
  14. date命令转换日期命令提示date: illegal time format
  15. AngularJS 启动执行过程
  16. 斯巴达克斯诅咒者之战第三季/全集Spartacus迅雷下载
  17. 九度 1557:和谐答案 (LIS 变形)
  18. pymssql
  19. async await promise 执行时序
  20. Vue组件的定义方式

热门文章

  1. linux nslookup-查询域名DNS信息的工具
  2. lucene-5.3.1配置(win7x64)
  3. C#NumberFormatInfo类
  4. Django 模版语法 二
  5. table JS合并单元格
  6. HTML基础知识 table中 th,td,tr
  7. PHP读取mysql中的数据
  8. POJ 1679 判最小生成树的不唯一性 或 利用次小生成树求解
  9. Android定位(是否使用GPS进行定位)
  10. 从零开始写STL-容器-list