洛谷P1351 联合权值(树形dp)
题意
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)的更多相关文章
- 洛谷 P1351 联合权值 —— 树形DP
题目:https://www.luogu.org/problemnew/show/P1351 树形DP,别忘了子树之间的情况(拐一下距离为2). 代码如下: #include<iostream& ...
- 洛谷 1351 联合权值——树形dp
题目:https://www.luogu.org/problemnew/show/P1351 对拍了一下,才发现自己漏掉了那种拐弯的情况. #include<iostream> #incl ...
- 洛谷 P1351 联合权值 题解
P1351 联合权值 题目描述 无向连通图 \(G\) 有 \(n\) 个点,\(n-1\) 条边.点从 \(1\) 到 \(n\) 依次编号,编号为 \(i\) 的点的权值为 \(W_i\),每条 ...
- [NOIP2014] 提高组 洛谷P1351 联合权值
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- 洛谷 P1351 联合权值
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- 洛谷——P1351 联合权值
https://www.luogu.org/problem/show?pid=1351 题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i , ...
- 『题解』洛谷P1351 联合权值
更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 无向连通图\(\mathrm G\)有\(n\)个点,\(n - 1\)条边.点从 ...
- 洛谷P1351 联合权值
\(\Large\textbf{Description:}\) \(\large一棵树,父子之间距离为1,求距离为2的两点点权之积的最大值与和.\) \(\Large\textbf{Solution: ...
- P1351 联合权值(树形dp)
P1351 联合权值 想刷道水题还交了3次.....丢人 (1.没想到有两个点都是儿子的状况 2.到处乱%(大雾)) 先dfs一遍处理出父亲$fa[x]$ 蓝后再一遍dfs,搞搞就出来了. #incl ...
随机推荐
- 二分查找法C语言实现
[问题描述] 生成一个随机数组A[64] ,在数组中查找是否存在某个数num. [答案] #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> ...
- 在请求中存取属性setAttribute&getAttribute方法
在请求中保存属性: public void setAttribute(String name,Object o) request.setAttribute("mess"," ...
- selenium+PhantomJS简单爬虫
#!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on 2017年10月19日 @author: zzy ''' import tim ...
- 用python的正则表达式实现简单的计算器功能
#!/usr/bin/env python # -*- coding:utf-8 -*- import sys import re def welcome_func(): ""&q ...
- P4854 MloVtry的咸鱼树 状压+最短路
$ \color{#0066ff}{ 题目描述 }$ 俗话说种瓜得瓜,种豆得豆,MloVtry把自己砍掉一半埋进了土里,于是它得到了一颗n个点的咸鱼树. 但是问题是由于MloVtry只舍得埋下一半的自 ...
- 6A - Daydreamin
#include <iostream> #include <cstdio> using namespace std; typedef long long ll; ll dp[] ...
- jQuery Validate验证框架详解(jquery.validate.min.js)
原博客 jQuery Validate验证框架详解 jQuery校验官网地址:https://jqueryvalidation.org/ 一.导入js库 <script type="t ...
- Qt 学习之路 2(69):进程
Qt 学习之路 2(69):进程 豆子 2013年11月9日 Qt 学习之路 2 15条评论 进程是操作系统的基础之一.一个进程可以认为是一个正在执行的程序.我们可以把进程当做计算机运行时的一个基础单 ...
- LeetCode8. 字符串转整数 (atoi)
8. 字符串转整数 (atoi) 描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连 ...
- C++标准库之String
C++中支持的字符串处理的函数库叫String,但它不是STL,却与STL操作十分相似. 1.声明: 使用String之前要有以下头文件 #include<string> using na ...