\(\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 联合权值的更多相关文章

  1. 洛谷 P1351 联合权值 题解

    P1351 联合权值 题目描述 无向连通图 \(G\) 有 \(n\) 个点,\(n-1\) 条边.点从 \(1\) 到 \(n\) 依次编号,编号为 \(i\) 的点的权值为 \(W_i\)​,每条 ...

  2. [NOIP2014] 提高组 洛谷P1351 联合权值

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  3. 洛谷 P1351 联合权值

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  4. 洛谷——P1351 联合权值

    https://www.luogu.org/problem/show?pid=1351 题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i , ...

  5. 『题解』洛谷P1351 联合权值

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 无向连通图\(\mathrm G\)有\(n\)个点,\(n - 1\)条边.点从 ...

  6. 洛谷P1351 联合权值(树形dp)

    题意 题目链接 Sol 一道很简单的树形dp,然而被我写的这么长 分别记录下距离为\(1/2\)的点数,权值和,最大值.以及相邻儿子之间的贡献. 树形dp一波.. #include<bits/s ...

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

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

  8. 洛谷 1351 联合权值——树形dp

    题目:https://www.luogu.org/problemnew/show/P1351 对拍了一下,才发现自己漏掉了那种拐弯的情况. #include<iostream> #incl ...

  9. P1351 联合权值(树形dp)

    P1351 联合权值 想刷道水题还交了3次.....丢人 (1.没想到有两个点都是儿子的状况 2.到处乱%(大雾)) 先dfs一遍处理出父亲$fa[x]$ 蓝后再一遍dfs,搞搞就出来了. #incl ...

随机推荐

  1. T4 多文件生成说明

    1.安装T4,自动生成文件 Manager.ttinclude <#@ assembly name="System.Core"#> <#@ assembly na ...

  2. Dart语言学习(十四) Dart泛型

    什么是泛型? 通俗理解:泛型就是解决 类 接口 方法的复用性.以及对不特定数据类型的支持(类型校验) 如下代码,只能返回string类型的数据 String getData(String value) ...

  3. MRCP接口MRCPRecog 简介

    功能:开始一个语音识别,一边讲话,一边识别,需要ASR服务器. 原型:MRCPRecog (grammar, options) grammar ---- 语法文件,可以是一个xml文件 options ...

  4. 【转】十步让你成为一名优秀的Web开发人员

    第一步:学好HTML HTML(超文本标记语言)是网页的核心,因此你首先应该学好它,不要害怕,HTML很容易学习的,但也很容易误用,学懂容易要学精还得费点功夫,但学好HTML是成为Web开发人员的基本 ...

  5. 算法-leetcode-65-Valid Number

    算法-leetcode-65-Valid Number 上代码: # coding:utf-8 __author__ = "sn" """Valida ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:让表格更加紧凑

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. Atcoder Beginner Contest 139E(模拟,思维)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int n;int a[1007][1007] ...

  8. Linux centosVMware shell中的函数、shell中的数组、

    一.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可. 格式: function _name() { command ...

  9. 「AHOI2008」紧急集合/聚会

    题目描述 这次也是很长的题面啊\(qwq\) 题目大意如下: 给定一棵\(N\)个节点的树以及\(M\)次询问,每次询问给出\(x,\ y,\ z\)三个节点,程序需要在树上找一个点\(p\) 使得\ ...

  10. alibaba sentinel限流组件 源码分析

    如何使用? maven引入: <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>s ...