By S_Aditya, history, 3 years ago, I have recently learnt the rerooting technique from the previous contest. I will be greatful if someone will please share some problems related to it. Comments (28)
 »
 » thanks
 »
•  » » » 2 years ago, # ^ | ← Rev. 2 →   ok (clan like some group for practicing algos and problems i assume).
•  » » » »
 » 2 years ago, # | ← Rev. 3 →   1187E - Tree Painting One of the best problem on rerooting!!
 » This comment have few more nice problems on rerooting.
 » This one was fun to work out: https://codeforces.com/contest/960/problem/E
 » this one: Atcoder dp_v
•  » » if we use rerooting in this problem, won't we have to use modular inverse at some point, and it is not neccesary that the modular inverse would exist with respect to m.(please correct me if I am wrong)
•  » » » No, for an array if we want to know for every number, the product of other numbers, we can achieve it by storing the prefix product and suffix product.
•  » » » » can you please elaborate more or share the link of your code
•  » » » » » Here you go: submissiomNOTE: My code is a little ugly.
•  » » » » » ![](https://i.imgur.com/2vVTfEs.jpg) Instead of dividing the product of the whole array by x, we can compute prefix * suffix
 » Can Someone explain What is rerooting?
•  » » In rerooting, what we basically do is first calculate the required data by considering some node as the root of the tree.In rerooting type problems, it is required to calculate the same data by considering each and every node as the root, using naive approach would result in tle.So, what we try do is using the calculated values(say by considering 1 to be the root), we try to calculate the answer for each node(if we assume that node to be the root) in O(1) time.Consider a case in which node is the current root and it is one of it's child, so it would had contributed something in the answer of node, so now we try to make it as the new root, so first we remove the contribution of it from node, then node is now a child for it, so we add the contribution of the subtree with root being node to the answer of it and now it has become the new root of the tree.But when we need to calculate the answer for say it1(second child of node) , we have to rollback the changes we made.
 » The blog from where I learnt rerooting:-link
 » One more problem based on the concept : 1324F - Maximum White Subtree
 »
 »