S_Aditya's blog

By S_Aditya, history, 5 years ago, In English

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.

  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

thanks

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
5 years ago, # |
Rev. 3   Vote: I like it +5 Vote: I do not like it

1187E - Tree Painting One of the best problem on rerooting!!

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

This comment have few more nice problems on rerooting.

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

This one was fun to work out: https://codeforces.net/contest/960/problem/E

»
5 years ago, # |
  Vote: I like it +16 Vote: I do not like it

this one: Atcoder dp_v

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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)

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +19 Vote: I do not like it

      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.

»
5 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Can Someone explain What is rerooting?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    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.

»
5 years ago, # |
  Vote: I like it +2 Vote: I do not like it

The blog from where I learnt rerooting:-link

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

One more problem based on the concept : 1324F - Maximum White Subtree

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

This is quite a nice one!

»
3 years ago, # |
Rev. 3   Vote: I like it +6 Vote: I do not like it

More problems from recent ABCs that can be solved with rerooting:

https://atcoder.jp/contests/abc223/tasks/abc223_g

https://atcoder.jp/contests/abc222/tasks/abc222_f

https://atcoder.jp/contests/abc220/tasks/abc220_f

In particular abc222f's editorial has a nice generic template with links to (japanese) tutorials

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hello,

    Actually I can't understand how to use merge and apply function

    Like in your second question I am using

    Data merge(Data a, Data b) { return a + b; }

    and apply as

    Data apply(Data a, int c, int d, Cost w) { return a + w; }

    But this is wrong