\(\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. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

  2. 操作系统OS - 反置页表

    1. https://blog.csdn.net/wuyuegb2312/article/details/16359821 2. https://www.youtube.com/watch?v=YQ3 ...

  3. 【协作式原创】查漏补缺之Go并发问题(单核多核)

    主要回答一下几个问题 1.单核并发问题 2.多核并发问题 2.几个不正确的同步案例 1.单核并发问题 先看一段go(1.11)代码: 单核CPU,1万个携程,每个携程执行100次+1操作, 思考n最终 ...

  4. 五 Action访问方法,method配置,通配符(常用),动态

    1 通过method配置(有点low) 建立前端JSP:demo4.jsp <%@ page language="java" contentType="text/h ...

  5. Unity初步 基本拼图实现

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...

  6. Android SDCard文件、目录操作【转】

    一.权限问题 参考:http://www.cnblogs.com/sky-zhang/p/3403393.html Android框架是基于Linux内核构建,所以Android安全系统也是基于Lin ...

  7. 「CQOI2011」动态逆序对

    「CQOI2011」动态逆序对 传送门 树套树. 删除一个位置的元素带来的减损数等于他前面大于它的和后面小于它的,然后这个直接树状数组套主席树维护一下就好了. 参考代码: #include <c ...

  8. Python 基础之推导式

    一.列表推导式 通过一行循环判断,遍历出一系列数据的方式就是推导式 特点:方便,简洁,可以实现一些简单的功能推导式当中只能跟循环和判断(单项分支)种类分为三种: 列表推导式  集合推导式  字典推导式 ...

  9. Scrapy 中的 Request 对象和 Respionse 对象

    1.Request 对象 Request 对象用来描述一个 HTTP 请求,下面是其构造方法的参数列表 Request(url, [, callback, method='Get', headers, ...

  10. Docker常用命令(命令大全)

    容器生命周期管理 1. docker run:创建一个新的容器并运行一个命令 2. Docker start/stop/restart - docker start:启动一个或多个已经被停止的容器 - ...