GCD on Tree
All submissions for this problem are available.
Given a tree with N nodes and N-1 edges, where each edge has a weight associated with it.
Gcd(A,B) represents gcd of weights of all the edges in the path from A to B.
Len(A,B) is number of edges in path from A to B.
You have to find maximum value of Gcd(A,B) * Len(A,B) over all possible pair of nodes A,B.
- The first line of the input contains an integer T denoting the number of test cases.
- The first line of each test case contains a single integer N denoting the number of nodes in the tree.
- Next N-1 lines contains 3 integers a,b,c denoting that there is an edge between node a and b having weight c.
- For each test case, output a single line containing the required answer.
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 100000
- 1 ≤ a,b ≤ N
- 1 ≤ c ≤ 100000
Input: 1 3 1 2 10 2 3 4 Output: 10
There are 3 possible paths: f(1,2) = 10*1 = 10 f(2,3) = 4*1 = 4 f(1,3) = 2*2 = 4 So the maximum value is 10
|Tags||centroid-decomp, data-structure, gcd, ipc151a, skullcrackers|
|Time Limit:||10 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, PYP3, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.