A16408 | Optimizations From Chelsu
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree with $n$ vertices, whose vertices are numbered from $1$ to $n$ . Each edge is labeled with some integer $w_i$ .
Define $len(u, v)$ as the number of edges in the simple path between vertices $u$ and $v$ , and $gcd(u, v)$ as the Greatest Common Divisor of all numbers written on the edges of the simple path between vertices $u$ and $v$ . For example, $len(u, u) = 0$ and $gcd(u, u) = 0$ for any $1 \leq u \leq n$ .
You need to find the maximum value of $len(u, v) \cdot gcd(u, v)$ over all pairs of vertices in the tree.
Define $len(u, v)$ as the number of edges in the simple path between vertices $u$ and $v$ , and $gcd(u, v)$ as the Greatest Common Divisor of all numbers written on the edges of the simple path between vertices $u$ and $v$ . For example, $len(u, u) = 0$ and $gcd(u, u) = 0$ for any $1 \leq u \leq n$ .
You need to find the maximum value of $len(u, v) \cdot gcd(u, v)$ over all pairs of vertices in the tree.
输入格式
Each test consists of multiple test cases. The first line contains a single integer $t$ ( $1 \leq t \leq 10^4$ ) — the number of test cases. This is followed by their description.
The first line of each test case contains the number $n$ ( $2 \leq n \leq 10^5$ ) — the number of vertices in the tree.
The next $n-1$ lines specify the edges in the format $u$ , $v$ , $w$ ( $1 \leq u, v \leq n$ , $1 \leq w \leq 10^{12}$ ).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$ .
The first line of each test case contains the number $n$ ( $2 \leq n \leq 10^5$ ) — the number of vertices in the tree.
The next $n-1$ lines specify the edges in the format $u$ , $v$ , $w$ ( $1 \leq u, v \leq n$ , $1 \leq w \leq 10^{12}$ ).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$ .
输出格式
For each test case, output a single number equal to the maximum value of $len(u, v) \cdot gcd(u, v)$ over all pairs of vertices in the tree.
输入输出样例
输入 #1
4 2 1 2 1000000000000 4 3 2 6 2 1 10 2 4 6 8 1 2 12 2 3 9 3 4 9 4 5 6 5 6 12 6 7 4 7 8 9 12 1 2 12 2 3 12 2 4 6 2 5 9 5 6 6 1 7 4 4 8 12 8 9 4 8 10 12 2 11 9 7 12 9
输出 #1
1000000000000 12 18 24
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted