题意

题目链接

Sol

一道很简单的树形dp,然而被我写的这么长

分别记录下距离为\(1/2\)的点数,权值和,最大值。以及相邻儿子之间的贡献。

树形dp一波。。

#include<bits/stdc++.h>
#define Fin(x) {freopen(x, "r", stdin);}
#define int long long
using namespace std;
const int MAXN = 2e5 + 10, mod = 10007;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, a[MAXN];
vector<int> v[MAXN];
int f[MAXN][3], sum[MAXN][3], num[MAXN][3], dis[MAXN], down[MAXN], Son[MAXN];
int add(int x, int y) {
if(x + y < 0) return x + y + mod;
else return x + y >= mod ? x + y - mod : x + y;
}
int mul(int x, int y) {
return 1ll * x * y % mod;
}
void dfs(int x, int fa) {
dis[x] = 0;
for(int i = 0, to; i < v[x].size(); i++) {
if((to = v[x][i]) == fa) continue;
dfs(to, x);
dis[x] = add(dis[x], mul(sum[x][1], a[to]));
if(!Son[x]) Son[x] = a[to];
else down[x] = max(down[x], a[to] * Son[x]), Son[x] = max(Son[x], a[to]);
f[x][1] = max(f[x][1], a[to]);
f[x][2] = max(f[x][2], f[to][1]);
sum[x][1] = add(sum[x][1], a[to]);
sum[x][2] = add(sum[x][2], sum[to][1]);
num[x][1]++;
num[x][2] += num[to][1];
}
}
signed main() {
memset(f, -1, sizeof(f));
N = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
for(int i = 1; i <= N; i++) a[i] = read();
dfs(1, 0);
int mx = 0, s = 0;
for(int i = 1; i <= N; i++) {
if(~f[i][2]) mx = max(mx, a[i] * f[i][2]);
if(num[i][2]) s = add(s, mul(a[i], sum[i][2]));
if(num[i][1] > 1) s = add(s, dis[i]), mx = max(mx, down[i]);
}
cout << mx << " " << s * 2 % mod;
return 0;
}
/*
*/

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

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

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

  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. 学习markdown语法,易读易写,放2个教程地址

    http://wowubuntu.com/markdown/basic.html http://wowubuntu.com/markdown/basic.html

  2. Tomcat 配置文件的解析

    转载:https://www.cnblogs.com/sunshine-1/p/8990044.html https://www.cnblogs.com/kismetv/p/7228274.html ...

  3. Android中include标签的使用(打开引用布局,隐藏当前布局)

    在开发app的时候,有时候一个布局会反复用到,可以把反复用到的布局单独写一个xml文件,什么时候用到就用includ标签引入xml 下面是我写的反复用到的一个xml,里面有2个button,一个Tex ...

  4. shim和polyfill,前端术语

    最近项目临近发布,JS的bug大都修改完毕,终于进入了我在这家公司实习+入职为数不多的摸鱼时刻.(想想真是有点感人啊) 因为项目要兼容IE8,所以我们的代码里常常要用到 shim 以支持ES5 的相关 ...

  5. thinkphp3.2.3 批量包含文件

    自己瞎写的...凑合看吧...核心就是用正则 表达式 或者 字符串 str_replace 进行 替换....... /** * 批量包含---,不能递归包含!!! 请不要在目标目录 包含 文件夹,因 ...

  6. 【算法笔记】B1016 部分A+B

    1016 部分A+B (15 分) 正整数 A 的“D​A​​(为 1 位整数)部分”定义为由 A 中所有 D​A​​ 组成的新整数 P​A​​.例如:给定 A=3862767,D​A​​=6,则 A ...

  7. Loj 6433. 「PKUSC2018」最大前缀和 (状压dp)

    题面 Loj 题解 感觉挺难的啊- 状压\(dp\) 首先,有一个性质 对于一个序列的最大前缀和\(\sum_{i=1}^{p} A[i]\) 显然对于每个\(\sum_{i=p+1}^{x}A[i] ...

  8. LeetCode936. Stamping The Sequence

    一.题面 You want to form a target string of lowercase letters. At the beginning, your sequence is targe ...

  9. 找出数组中的最小值(es5/es6)

    1.命令式编程,只需要迭代数组,检查当前最小值是否大于数组元素,如果是更新最小值即可. var s = [2,3,4,5,6,7,8]; for(var i=0,m=s.length;i<m;i ...

  10. HDU4499

    In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or ...