P1351 联合权值

想刷道水题还交了3次.....丢人

(1.没想到有两个点都是儿子的状况 2.到处乱%(大雾))

先dfs一遍处理出父亲$fa[x]$

蓝后再一遍dfs,搞搞就出来了。

#include<iostream>
#include<cstdio>
#include<cstring>
#define re register
using namespace std;
const int p=;
int max(int &a,int &b){return a>b?a:b;}
#define N 200002
int n,fa[N],val[N],f1[N],f2[N];
int cnt,hd[N],nxt[N<<],ed[N],poi[N<<];
void adde(int x,int y){
nxt[ed[x]]=++cnt; hd[x]=hd[x]?hd[x]:cnt;
ed[x]=cnt; poi[cnt]=y;
}
void dfs1(int x,int ffa){//处理fa数组
fa[x]=ffa;
for(int i=hd[x];i;i=nxt[i])
if(poi[i]!=ffa)
dfs1(poi[i],x);
}
void dfs2(int x){
int mxd=,tot=;
for(int i=hd[x];i;i=nxt[i]){
int to=poi[i];
if(to==fa[x]) continue;
f1[x]=max(f1[x],val[to]*mxd);
f2[x]=1ll*(f2[x]+val[to]*tot)%p;
mxd=max(mxd,val[to]);//以上为构成联合权值的2个点都是儿子的情况
tot=(tot+val[to])%p;
dfs2(to);
f1[x]=max(f1[x],f1[to]);
f2[x]=1ll*(f2[x]+f2[to])%p;
}
int g=fa[fa[x]],v=val[x]*val[g];//点x和x的爷爷构成联合权值
f1[g]=max(f1[g],v);
f2[g]=1ll*(f2[g]+v)%p;
}
int main(){
scanf("%d",&n); int q1,q2;
for(int i=;i<n;++i){
scanf("%d%d",&q1,&q2);
adde(q1,q2); adde(q2,q1);
}
for(int i=;i<=n;++i) scanf("%d",&val[i]);
dfs1(,); dfs2(); f2[]=1ll*f2[]*%p;//记得*2
printf("%d %d",f1[],f2[]);
return ;
}

P1351 联合权值(树形dp)的更多相关文章

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

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

  2. 洛谷 P1351 联合权值 —— 树形DP

    题目:https://www.luogu.org/problemnew/show/P1351 树形DP,别忘了子树之间的情况(拐一下距离为2). 代码如下: #include<iostream& ...

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

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

  4. 洛谷 P1351 联合权值 题解

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

  5. P1351 联合权值[鬼畜解法]

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

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

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

  7. 【NOIP2014】联合权值 树上dp

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

  8. luogu P1351 联合权值

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

  9. 洛谷 P1351 联合权值

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

随机推荐

  1. js中字符串支持正则表达式的方法

    设一个字符串var myName = "fangming";则支持正则表达式的方法有:split(分割),replace(替换),search(查找),match(元素参数的数组) ...

  2. poj_1182 并查集

    题目大意 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B,B吃C,C吃A.现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人 ...

  3. Egret打包App 短暂黑屏解决方案 (Egret4.1.0)

    论坛已经有人解决:http://bbs.egret.com/forum.php?mod=viewthread&tid=30288&highlight=app%2B%E9%BB%91%E ...

  4. 【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn 可并堆

    [BZOJ3011][Usaco2012 Dec]Running Away From the Barn Description It's milking time at Farmer John's f ...

  5. SpringBoot系列教程起步

    本篇学习目标 Spring Boot是什么? 构建Spring Boot应用程序 三分钟开发SpringBoot应用程序 本章源码下载 Spring Boot是什么? spring Boot是由Piv ...

  6. Centos6.8 防火墙设置

    1.指令 vi /etc/sysconfig/iptables 添加以下内容和要开放的端口 # Firewall configuration written by system-config-fire ...

  7. js jquery.pagination.js分页

    1.使用插件为 jquery.pagination.js ,如果没有这个js文件的话,我可以给发个. 首先引用 jquery.pagination.js (分页js),跟pagination.css( ...

  8. pta 习题集5-19 列车厢调度

    1 ====== <--移动方向 / 3 ===== \ 2 ====== -->移动方向 大家或许在某些数据结构教材上见到过"列车厢调度问题"(当然没见过也不要紧). ...

  9. 徐州网络赛J-Maze Designer【最小生成树】【LCA】

    After the long vacation, the maze designer master has to do his job. A tour company gives him a map ...

  10. Quartz学习记录

    参考资料: 官方网站 Quartz使用总结