洛谷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.62——不同路径
问题描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为 ...
- uniGUI之UniSyntaxEdit(24)
UniSyntaxEdit1语法高亮显示控件,主要属性Language,它是 多行 1]Language 语言 2]执行 FDquery1.Open(UniSyntaxEdit1.Lines.Tex ...
- MySQL - 在Ubuntu下密码初始化
1. 打开/etc/mysql/debian.cnf文件,在这个文件中有系统默认给我们分配的用户名和密码,通过这个密码就可以直接对MySQL进行操作了. 2. 以debian-sys-maint为用户 ...
- postman使用get请求的url地址传参中文乱码问题
编码之后
- PAT T1014 Circles of Friends
大水题,dfs判连通块的数量,bfs每个点找朋友圈的最大直径~ #include<bits/stdc++.h> using namespace std; ; vector<int&g ...
- python 基础之字符串方法
字符串 print('chenxi'*8) 测试 D:\python\python.exe D:/untitled/dir/for.py chenxichenxichenxichenxichenxic ...
- 【剑指Offer面试编程题】题目1507:不用加减乘除做加法--九度OJ
题目描述: 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入为两个整数m和n(1<=m,n<=10 ...
- 02-06Android学习进度报告六
今天学习了关于Android开发中常用的两个知识,即对话框和悬浮框. 首先我学习了对话框的基本使用流程 Step 1:创建AlertDialog.Builder对象: Step 2:调用setIcon ...
- 微信授权登陆接入第三方App(步骤总结)Android。
这几天开发要用到微信授权的功能,所以就研究了一下.可是微信开放平台接入指南里有几个地方写的不清不楚.在此总结一下,以便需要的人. 很多微信公众平台的应用如果移植到app上的话就需要微信授权登陆了. 目 ...
- IDEA导入项目后,导入artifacts 方法 以及 Spring的配置文件找不到的解决方法
我们一般选择 open 项目,如果没有artifacts 的添加选项,我们就要选择 import 项目. 如果没有artifacts ,项目下面会有错误提示,点击错误提示Fix,设置里面导入artif ...