题意

题目链接

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. gym 102082G BZOJ4240 贪心+树状数组

    4240: 有趣的家庭菜园 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 756  Solved: 349[Submit][Status][Discu ...

  2. vue-persist 为 vuex 持久化!!

    npm install --save vuex-persist import VuexPersistence from 'vuex-persist' const vuexLocal = new Vue ...

  3. POJ_3262 Protecting the Flowers 【贪心】

    一.题面 POJ3262 二.分析 这题要往贪心上面想应该还是很容易的,但问题是要证明为什么比值关系就能满足. 可以选择几个去分析,入1-6  与 2-15  和 1-6 与2-5 和 1-6 与 2 ...

  4. POJ_2456 Aggressive cows 【二分求最大化最小值】

    题目: Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are l ...

  5. Just a Hook(线段树区间修改值)-------------蓝桥备战系列

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  6. python-继承,父类,子类

    class Spell(object): def __init__(self, incantation, name): self.name = name self.incantation = inca ...

  7. springboot(五)-使用Redis

    Redis服务器 springboot要使用redis,首先当然要确保redis服务器能够正常跑起来. pom.xml 这里添加redis的依赖,当然也是springboot集成好的. <!-- ...

  8. 学习掌握oracle外表(external table)

    [转自] http://blog.chinaunix.net/uid-10697776-id-2935685.html 定义 External tables access data in extern ...

  9. JAVA 序列化_基础

    JAVA序列化 实现 Serializable 接口的对象,可以序列化为本地文件 简单示例: //序列化类 public class Test implements Serializable { pr ...

  10. Vue PDF文件预览vue-pdf

       最近做项目,遇到预览PDF这个功能,在网上找了找,大多推荐的是pdf.js,不过在Vue中还是想偷懒直接npm组件,最后找到了一个还不错的Vue-pdf 组件,GitHub地址:https:// ...