CF1916G.Optimizations From Chelsu

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree with nn vertices, whose vertices are numbered from 11 to nn. Each edge is labeled with some integer wiw_i.

Define len(u,v)len(u, v) as the number of edges in the simple path between vertices uu and vv, and gcd(u,v)gcd(u, v) as the Greatest Common Divisor of all numbers written on the edges of the simple path between vertices uu and vv. For example, len(u,u)=0len(u, u) = 0 and gcd(u,u)=0gcd(u, u) = 0 for any 1≤u≤n1 \leq u \leq n.

You need to find the maximum value of len(u,v)⋅gcd(u,v)len(u, v) \cdot gcd(u, v) over all pairs of vertices in the tree.

给你一棵包含 nn 个顶点的树,顶点编号为 11 到 nn。每条边都标有一个整数 wiw_i。

定义 len(u,v)len(u, v) 为顶点 uu 与 vv 之间简单路径上的边数,gcd(u,v)gcd(u, v) 为顶点 uu 与 vv 之间简单路径上所有边所标数字的最大公约数。例如,对任意 1≤u≤n1 \leq u \leq n,均有 len(u,u)=0len(u, u) = 0 且 gcd(u,u)=0gcd(u, u) = 0。

你需要在树中所有顶点对中,找出 len(u,v)⋅gcd(u,v)len(u, v) \cdot gcd(u, v) 的最大值。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041 \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 nn (2≤n≤1052 \leq n \leq 10^5) — the number of vertices in the tree.

The next n−1n-1 lines specify the edges in the format uu, vv, ww (1≤u,v≤n1 \leq u, v \leq n, 1≤w≤10121 \leq w \leq 10^{12}).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \leq t \leq 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤1052 \leq n \leq 10^5),表示树中顶点的数量。

接下来的 n−1n-1 行以格式 uu, vv, ww(1≤u,v≤n1 \leq u, v \leq n,1≤w≤10121 \leq w \leq 10^{12})指定边。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case, output a single number equal to the maximum value of len(u,v)⋅gcd(u,v)len(u, v) \cdot gcd(u, v) over all pairs of vertices in the tree.

对于每个测试用例,输出一个单一数字,该数字等于树中所有顶点对 (u,v)(u, v) 的 len(u,v)⋅gcd(u,v)len(u, v) \cdot gcd(u, v) 的最大值。

输入输出样例

  • 输入#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

输入解题思路,AI测评打分。不知道怎么写?

首页