有点像计蒜之道里的 京东的物流路径

题目描述

给定一棵 N 个节点的树,每个节点有一个正整数权值。记节点 i 的权值为 Ai。
考虑节点 u 和 v 之间的一条简单路径,记 dist(u, v) 为其长度,gcd(u, v) 为路径上所有节点
(包含 u 和 v)的权值的最大公因子。min(u, v) 为路径上所有节点的权值的最小值。
请求出所有节点对 (u, v) 中 dist(u, v) · gcd(u, v) · min(u, v) 的最大值

输入格式

输入的第一行包含一个整数 T,代表测试数据的组数。接下来是 T 组数据。
每组数据的第一行包含一个整数 N,代表树中节点的个数。接下来一行包含 N 个整数
A1, A2, . . . , AN。
接下来 N − 1 行,每行包含三个整数 u, v, w,代表节点 u 和 v 之间连有一条长度为 w 的边。

输出格式

对于每组数据,输出一行,包含一个整数,代表所求答案

数据范围

• 1 ≤ T ≤ 100
• 2 ≤ N ≤ 10^5
• 2 ≤ ∑N ≤ 10^5
• 1 ≤ Ai ≤ 10^4
• 1 ≤ u, v ≤ N
• 1 ≤ w ≤ 10^5


题目分析

常规做法

和京东的物流路径不同的是,需要在外层枚举路径的gcd,并把两端点是gcd倍数的边存下,按照端点权值的较小值排序。之后就相当于是用这些边来和那题一样做了。

参见:[树的直径] Codechef March Cook-Off 2018. Maximum Tree Path

启发式搜索

我也不知道复杂度是多少

每一次搜索时若$dia*mn*gcd(dia为直径)<ans$就return。

 #include<bits/stdc++.h>
typedef long long ll;
const int maxn = ; struct Edge
{
int y,val;
Edge(int a=, int b=):y(a),val(b) {}
}edges[maxn<<];
long long ans;
int tt,n,dia,S,T,a[maxn],dis[maxn];
int edgeTot,head[maxn],nxt[maxn<<]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
int gcd(int a, int b){return !b?a:gcd(b, a%b);}
void addedge(int u, int v)
{
int c = read();
edges[++edgeTot] = Edge(v, c), nxt[edgeTot] = head[u], head[u] = edgeTot;
edges[++edgeTot] = Edge(u, c), nxt[edgeTot] = head[v], head[v] = edgeTot;
}
void dfs(int x, int fa, int c)
{
dis[x] = c;
for (int i=head[x]; i!=-; i=nxt[i])
if (edges[i].y!=fa) dfs(edges[i].y, x, c+edges[i].val);
}
void fnd(int x, int fa, ll d, int g, int mn)
{
if (1ll*dia*g*mn <= ans) return;
if (ans < 1ll*d*g*mn) ans = 1ll*d*g*mn;
for (int i=head[x]; i!=-; i=nxt[i])
if (edges[i].y!=fa)
fnd(edges[i].y, x, d+1ll*edges[i].val, gcd(g, a[edges[i].y]), std::min(mn, a[edges[i].y]));
}
void fndDiameter()
{
S = T = , dis[] = -0x3f3f3f3f;
dfs(, , );
for (int i=; i<=n; i++)
if (dis[S] < dis[i]) S = i;
dfs(S, S, );
for (int i=; i<=n; i++)
if (dis[T] < dis[i])
T = i, dia = dis[T];
fnd(S, S, , a[S], a[S]);
}
int main()
{
tt = read();
while (tt--)
{
n = read(), ans = dia = edgeTot = ;
memset(head, -, (n+)<<);
for (int i=; i<=n; i++) a[i] = read();
for (int i=; i<n; i++) addedge(read(), read());
fndDiameter();
for (int i=; i<=n; i++)
fnd(i, i, , a[i], a[i]);
printf("%lld\n",ans);
}
return ;
}

END

【启发式搜索】Codechef March Cook-Off 2018. Maximum Tree Path的更多相关文章

  1. Codechef March Cook-Off 2018. Maximum Tree Path

    目录 题意 解析 AC_code @(Codechef March Cook-Off 2018. Maximum Tree Path) 题意 给你一颗\(n(1e5)\)个点有边权有点权的树,\(Mi ...

  2. 2018.7中石油个人赛第4场(D-Transit Tree Path)-最短路算法

    6690: Transit Tree Path 时间限制: 1 Sec  内存限制: 128 MB提交: 472  解决: 132[提交] [状态] [讨论版] [命题人:admin] 题目描述 Yo ...

  3. hdu 1839 Delay Constrained Maximum Capacity Path 二分/最短路

    Delay Constrained Maximum Capacity Path Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu. ...

  4. HUSTOJ:Transit Tree Path

    问题 D: Transit Tree Path   You are given a tree with N vertices.Here, a tree is a kind of graph, and ...

  5. Lintcode376-Binary Tree Path Sum-Easy

    376. Binary Tree Path Sum Given a binary tree, find all paths that sum of the nodes in the path equa ...

  6. hdu 1839 Delay Constrained Maximum Capacity Path(spfa+二分)

    Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65 ...

  7. CodeChef March Lunchtime 2018 div2

    地址https://www.codechef.com/LTIME58B?order=desc&sortBy=successful_submissions 简单做了一下,前三题比较水,第四题应该 ...

  8. Codechef March Challenge 2014——The Street

    The Street Problem Code: STREETTA https://www.codechef.com/problems/STREETTA Submit Tweet All submis ...

  9. [codechef July Challenge 2017] Pishty and tree

    PSHTTR: Pishty 和城堡题目描述Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格的古堡和非常聪明的熊闻名全国.胡斯特的镇城之宝是就是这么一座古堡,历 ...

随机推荐

  1. sublime text 3 的emmet 添加自定义 html 片段

    比如想在html写 jquery 直接添加jquery地址,打开 ‘首选项->Package Setting->Emmet->Settings - User’, css也可以如法炮制 ...

  2. python进阶05 常用问题库(1)json os os.path模块

    python进阶05 常用问题库(1)json os os.path模块 一.json模块(数据交互) web开发和爬虫开发都离不开数据交互,web开发是做网站后台的,要跟网站前端进行数据交互 1.什 ...

  3. BZOJ1057(单调栈 or 悬线法)

    方法一 黑白棋盘拥有性质:(r + c) % 2的值决定颜色 因此把某色全部反转,直接求另一色的最大矩形即可,单调栈的经典问题 #include <cstdio> #include < ...

  4. Rebus消息总线

    这里主要讲一下我基于Rebus写的一个ABP框架的模块   目录结构 对于Rebus网上的资料很少,其实我对于服务总线也不是很理解 ..个人理解的就是像ABP中的EventBus那样的,但是集成了一些 ...

  5. 安装Jaspersoft Studio

    下载位置:http://community.jaspersoft.com/project/jaspersoft-studio/releases.

  6. PIX 防火墙

    ---恢复内容开始--- 一 , PIX 防火墙的认识 PIX 是cisco 的硬件防火墙 硬件防火墙的工作速度快,使用方便. PIX 有很多型号,并发连接数是PIX防火墙的重要参数   PIX 25 ...

  7. STM32之ADC(内部基准电压,参考电压)

    转 STM32内部参照电压VREFIN的使用 https://blog.csdn.net/uncle_guo/article/details/50625660 每个STM32芯片都有一个内部的参照电压 ...

  8. SmartWeatherAPI C#版

    private string GetKey(string areaId, string type, string date, string appId, string privateKey) { va ...

  9. spark-2.2.0-bin-hadoop2.6和spark-1.6.1-bin-hadoop2.6发行包自带案例全面详解(java、python、r和scala)之Basic包下的JavaPageRank.java(图文详解)

    不多说,直接上干货! spark-1.6.1-bin-hadoop2.6里Basic包下的JavaPageRank.java /* * Licensed to the Apache Software ...

  10. 【踩坑】List 的陷阱

    今天测试iReview项目数据的反馈,发现有些语句总无法执行. 经过调试排查后,发现List<自定义类>返回了空集"[]",却无法进入if语句里面,即 if (List ...