题目: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 ;
}

洛谷 P1351 联合权值 —— 树形DP的更多相关文章

  1. 洛谷P1351 联合权值(树形dp)

    题意 题目链接 Sol 一道很简单的树形dp,然而被我写的这么长 分别记录下距离为\(1/2\)的点数,权值和,最大值.以及相邻儿子之间的贡献. 树形dp一波.. #include<bits/s ...

  2. 洛谷 1351 联合权值——树形dp

    题目:https://www.luogu.org/problemnew/show/P1351 对拍了一下,才发现自己漏掉了那种拐弯的情况. #include<iostream> #incl ...

  3. 洛谷 P1351 联合权值 题解

    P1351 联合权值 题目描述 无向连通图 \(G\) 有 \(n\) 个点,\(n-1\) 条边.点从 \(1\) 到 \(n\) 依次编号,编号为 \(i\) 的点的权值为 \(W_i\)​,每条 ...

  4. [NOIP2014] 提高组 洛谷P1351 联合权值

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  5. 洛谷 P1351 联合权值

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  6. 洛谷——P1351 联合权值

    https://www.luogu.org/problem/show?pid=1351 题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i , ...

  7. 『题解』洛谷P1351 联合权值

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 无向连通图\(\mathrm G\)有\(n\)个点,\(n - 1\)条边.点从 ...

  8. 洛谷P1351 联合权值

    \(\Large\textbf{Description:}\) \(\large一棵树,父子之间距离为1,求距离为2的两点点权之积的最大值与和.\) \(\Large\textbf{Solution: ...

  9. P1351 联合权值(树形dp)

    P1351 联合权值 想刷道水题还交了3次.....丢人 (1.没想到有两个点都是儿子的状况 2.到处乱%(大雾)) 先dfs一遍处理出父亲$fa[x]$ 蓝后再一遍dfs,搞搞就出来了. #incl ...

随机推荐

  1. linux命令 dig-域名查询工具

    博主推荐:更多网络测试相关命令关注 网络测试  收藏linux命令大全 dig命令是常用的域名查询工具,可以用来测试域名系统工作是否正常. 语法 dig(选项)(参数) 选项 @<服务器地址&g ...

  2. (十一)python3 encode()和decode()

    从英文意思上看,encode和decode分别指编码和解码.在python中,Unicode类型是作为编码的基础类型,即: decode encode str ---------> str(Un ...

  3. Vue如何使用vee-validate表单验证

    Vue项目遇到要表单验证了吧,对我来说表单验证是个很纠(dan)结(teng)的内容,各种判断凌乱到飞起.往常使用jquery的validate插件做表单验证方便吧,你也可以在Vue里引入jquery ...

  4. 基于html实现一个todolist待办事项

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  5. BNUOJ 9870 Contestants Division

    Contestants Division Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALiv ...

  6. 在ubuntu 18.04下装有线守护wg

    wg你懂的是什么,下面讲讲我这段时间安装过程,发觉这个软件和CENTOS 7下面不是很合拍,它更适合ubuntu,因为ubuntu仍然用iptables,而CENTOS 早就用了firewalld了, ...

  7. Codeforces698C. LRU

    n<=20种东西,有个大小k<=n的箱子,每次会以固定的概率从所有东西里选一种,若箱子里有空位且这种东西没出现过就丢进去,若箱子满了且这种东西没出现过就把最早访问过的一个丢掉,(只要在每次 ...

  8. 【重要】MySQL常见面试题

    1. 主键 超键 候选键 外键 主 键: 数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合.一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null). 超 键: 在关系中 ...

  9. QT-Embedded-4.5.3在海思35xx上移植

    QT4.5.3在海思3520A上移植步骤-修订版 2015年3月29日星期日, 16:59:03 1.首先要保证已经安装了海思的交叉编译器: #arm-hi  + Tab key to show wh ...

  10. Mayor's posters POJ - 2528

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...