洛谷P1351 联合权值
\(\Large\textbf{Description:}\) \(\large一棵树,父子之间距离为1,求距离为2的两点点权之积的最大值与和。\)
\(\Large\textbf{Solution:}\)\(\large考虑到边权为1,那么距离为2的两点可能问爷爷与孙子的关系,或者为兄弟的关系。那么对于一个点,它对答案的贡献是它所有孙子的点权之和与它的点权的积加上它与所有兄弟的有兄弟的点权的积。那么我们只需遍历一遍树即可,时间复杂度O(n)。\)
\(\Large\textbf{Code:}\)
#include <cstdio>
#include <algorithm>
#include <iostream>
#define LL long long
#define gc() getchar()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 2e5 + 5;
const int Mod = 10007;
int n, m, cnt, a[N], head[N];
LL ans, Max, sum[N];
struct Edge {
int to, next;
}e[N << 1];
struct Node {
LL su, max12; //开一个结构体 存储其孙子的点权之和与点权最大值。
Node() {
su = max12 = 0;
}
};
inline int read() {
char ch = gc();
int ans = 0, flag = 1;
while (ch > '9' || ch < '0') {
if (ch == '-') flag = -1;
ch = gc();
}
while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = gc();
return ans * flag;
}
inline void add(int l, int r) {
e[++cnt].to = r;
e[cnt].next = head[l];
head[l] = cnt;
}
inline LL max(LL x, LL y) {
return x >= y ? x : y;
}
inline Node dfs(int now, int fa) {
Node q, x;
LL an = 0, s = 0;
LL max1 = 0, max2 = 0;
for (int i = head[now]; i ; i = e[i].next) {
int u = e[i].to;
if (u != fa) {
an = an + a[u];
ans = (ans + sum[now] * a[u] % Mod) % Mod;
sum[now] = (sum[now] + a[u]) % Mod;
x = dfs(u, now);
s = s + x.su;
q.max12 = max(q.max12, a[u]);
if (a[u] > max1) max2 = max1, max1 = a[u];
else if (a[u] > max2) max2 = a[u];
}
else continue;
}
if (max2) Max = max(Max, max1 * max2);
Max = max(Max, x.max12 * a[now]);
ans = (ans + s * a[now] % Mod) % Mod;
q.su = an;
return q;
}
int main() {
n = read();
int l, r;
rep(i, 2, n) l = read(), r = read(), add(l, r), add(r, l);
rep(i, 1, n) a[i] = read();
dfs(1, 0);
ans = (ans << 1) % Mod;
printf("%lld %lld\n", Max, ans);
return 0;
}
\(\Large\color{pink}{by}\) \(\Large\color{pink}{Miraclys}\)
洛谷P1351 联合权值的更多相关文章
- 洛谷 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 联合权值(树形dp)
题意 题目链接 Sol 一道很简单的树形dp,然而被我写的这么长 分别记录下距离为\(1/2\)的点数,权值和,最大值.以及相邻儿子之间的贡献. 树形dp一波.. #include<bits/s ...
- 洛谷 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 联合权值(树形dp)
P1351 联合权值 想刷道水题还交了3次.....丢人 (1.没想到有两个点都是儿子的状况 2.到处乱%(大雾)) 先dfs一遍处理出父亲$fa[x]$ 蓝后再一遍dfs,搞搞就出来了. #incl ...
随机推荐
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- 1123. Lowest Common Ancestor of Deepest Leaves
link to problem Description: Given a rooted binary tree, return the lowest common ancestor of its de ...
- 机器学习之SVM多分类
实验要求数据说明 :数据集data4train.mat是一个2*150的矩阵,代表了150个样本,每个样本具有两维特征,其类标在truelabel.mat文件中,trainning sample 图展 ...
- Windows驱动开发-DPC定时器
DCP是一种使用更加灵活的定时器,可以对任意间隔时间进行定时.DPC定时器的内部使用了一个定时器对象KTIMER,当你设定了定时器之后,从设定开始起经过这个时间之后操作系统会将一个DPC定时器的例程插 ...
- [运维] 如何在 Linux 上安装 Nginx 服务器(一)
原因 因为小程序对素材的大小是由要求的, 所以为了简化小程序上的内存要求, 在Linux上安装nginx来作为静态资源服务器, 这篇为第一篇, 主要介绍怎么在Linux上安装nginx, 下一篇将会介 ...
- Windows一键启动多个软件
@echo off title 启动工作环境 @echo 正在启动FileZilla%start+空格+/d+空格+目录路径+空格+程序名 % start /d"F:\安装包\FileZil ...
- JavaScript一个简单的图片切换布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Swift-关于Swift编程语言
一.首先让我们看看苹果公司是怎么描述自己的Swift的: Swift 是编写程序的绝佳选择,无论是手机.电脑还是服务器,任何能跑代码的设备都是如此.它是一门集现代语言之大成,集结了苹果的工程师文化精髓 ...
- mabatisplus-update
/** * <p> * 根据 whereEntity 条件,更新记录 * </p> * * @param entity 实体对象 (set 条件值,不能为 null) * @p ...
- 为何以及如何学Linux系统?
在当今的社会中,linux用处实在是太过广泛了.现在用在服务器和嵌入式上的Linux发行版本数不胜数,桌面上linux只占1%的比例,但这不代表linux比windows和mac 做得差,实际上桌面系 ...