how to solve this problem?

problem link: https://www.hackerearth.com/problem/algorithm/gcd-on-tree-6c5cdfba/description/

how to apply centroid decomposition in this problem? or any other approach to solve this problem?

# | User | Rating |
---|---|---|

1 | tourist | 3557 |

2 | Radewoosh | 3468 |

3 | Um_nik | 3429 |

4 | Petr | 3354 |

5 | Benq | 3286 |

6 | mnbvmar | 3280 |

7 | wxhtxdy | 3276 |

7 | LHiC | 3276 |

9 | ecnerwala | 3214 |

10 | yutaka1999 | 3190 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 191 |

2 | Radewoosh | 180 |

3 | tourist | 173 |

4 | Vovuh | 165 |

4 | antontrygubO_o | 165 |

6 | PikMike | 164 |

7 | rng_58 | 160 |

8 | Um_nik | 156 |

8 | majk | 156 |

10 | 300iq | 154 |

how to solve this problem?

problem link: https://www.hackerearth.com/problem/algorithm/gcd-on-tree-6c5cdfba/description/

how to apply centroid decomposition in this problem? or any other approach to solve this problem?

↑

↓

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Oct/20/2019 15:48:18 (h1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Some Possibly Helpful Hints

1) The answer for leaves is 0.

2) Using the technique here, we can find the maximum GCD of a pair in an array with square root complexity.

Yes,I have previously seen this algorithm. But I actually didn't get it how to apply this algorithm in this question. Can you describe more how to solve? Thanks in advance.

For node pair(u,v) if we only update ans[LCA(u,v)] instead of all nodes on the path(u,v), the problem can be solved using DSU on tree. For the extended case, to get ans[u], we take all the subtrees of u (let the root of the chosen subtree be v, which must be a son of u) and update ans[u] with all nodes in tree(v) and out of tree(v).

Sorry for my poor English; the solution is not tested but I think it'd work. Hope it helps.

I discussed the problem with my friends, we think that centroid decomposition will work too. Insert each subtree of the current centroid and we can get the answer of the centroid. Note that clearing array must be finished by undo all the operations have done, or the complexity would be wrong.