【启发式搜索】Codechef March Cook-Off 2018. Maximum Tree Path
有点像计蒜之道里的 京东的物流路径
题目描述
给定一棵 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的更多相关文章
- Codechef March Cook-Off 2018. Maximum Tree Path
目录 题意 解析 AC_code @(Codechef March Cook-Off 2018. Maximum Tree Path) 题意 给你一颗\(n(1e5)\)个点有边权有点权的树,\(Mi ...
- 2018.7中石油个人赛第4场(D-Transit Tree Path)-最短路算法
6690: Transit Tree Path 时间限制: 1 Sec 内存限制: 128 MB提交: 472 解决: 132[提交] [状态] [讨论版] [命题人:admin] 题目描述 Yo ...
- hdu 1839 Delay Constrained Maximum Capacity Path 二分/最短路
Delay Constrained Maximum Capacity Path Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu. ...
- 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 ...
- 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 ...
- hdu 1839 Delay Constrained Maximum Capacity Path(spfa+二分)
Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65 ...
- CodeChef March Lunchtime 2018 div2
地址https://www.codechef.com/LTIME58B?order=desc&sortBy=successful_submissions 简单做了一下,前三题比较水,第四题应该 ...
- Codechef March Challenge 2014——The Street
The Street Problem Code: STREETTA https://www.codechef.com/problems/STREETTA Submit Tweet All submis ...
- [codechef July Challenge 2017] Pishty and tree
PSHTTR: Pishty 和城堡题目描述Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格的古堡和非常聪明的熊闻名全国.胡斯特的镇城之宝是就是这么一座古堡,历 ...
随机推荐
- JS高级学习历程-4
4 执行环境可以访问什么变量 具体可以访问变量类型:局部变量.参数.函数.外部环境变量 优先级:局部变量 > 函数 > 参数 > 外部环境变量 <!DOCTYPE html&g ...
- 《SQL 进阶教程》 case:在 UPDATE 语句里进行条件分支
1.对当前工资为30万日元以上的员工,降薪10%:2.对当前工资为25万日元以上且不满28万日元的员工,加薪20% update salaries set salary = case when sal ...
- [Android]解决 Could not read entry xxx from cache taskArtifacts.bin
Bug 出现 事情是这样的,昨天早晨我正做着项目,坐在我旁边的小伙伴呼唤了我一下,说项目运行不起来了. 我纳闷着,前天的时候还好好的,怎么过了一晚就出问题了.我问他是不是改了什么配置,或者添加了什么东 ...
- php:php相关的函数或用法记录
//1:判断字符串是否全是字母组成的 $str = 'AAKAaa_aLJIGF'; var_dump(ctype_alpha($str)); //boolean false,全部是英文时才是返回tr ...
- sql like 多条件
select * from student where name like 'mike%' or name like 'rose%';
- (转)io优化
原文:http://blog.csdn.net/gzh0222/article/details/9227393 1.系统学习 IO性能对于一个系统的影响是至关重要的.一个系统经过多项优化以后,瓶颈往往 ...
- spring data jpa 简单使用
通过解析方法名创建查询 通过前面的例子,读者基本上对解析方法名创建查询的方式有了一个大致的了解,这也是 Spring Data JPA 吸引开发者的一个很重要的因素.该功能其实并非 Spring Da ...
- If people in the communications only think about gains and losses of interest, then the pleasure of knowing each other will cease to exist.
If people in the communications only think about gains and losses of interest, then the pleasure of ...
- Country roads take me home, to the place I belong.
Country roads take me home, to the place I belong.故乡的路,带我回家吧,回到我期盼已久的归宿.
- AngularJS中最重要的核心功能
以下是AngularJS中最重要的核心功能: 数据绑定: 模型和视图组件之间的数据自动同步. 适用范围: 这些对象参考模型.它们充当控制器和视图之间的胶水. 控制器: 这些Javascript函数绑定 ...