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.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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.
Name |
---|
Codeforces Round #527 (Div. 3) Problem F
thanks
https://mirror.codeforces.com/contest/1324/problem/F
Well done, would you join our Clan!
ok (clan like some group for practicing algos and problems i assume).
https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/parwal-problem/
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://mirror.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: submissiom
NOTE: 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 andit
is one of it's child, soit
would had contributed something in the answer ofnode
, so now we try to makeit
as the new root, so first we remove the contribution ofit
fromnode
, thennode
is now a child forit
, so we add the contribution of the subtree with root beingnode
to the answer ofit
and nowit
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
766 E.
https://atcoder.jp/contests/abc160/tasks/abc160_f
https://atcoder.jp/contests/abc149/tasks/abc149_f
https://mirror.codeforces.com/contest/219/problem/D https://mirror.codeforces.com/problemset/problem/615/B
https://atcoder.jp/contests/abc160/tasks/abc160_f
https://atcoder.jp/contests/abc149/tasks/abc149_f
https://mirror.codeforces.com/contest/1187/problem/E
https://mirror.codeforces.com/contest/1092/problem/F
https://mirror.codeforces.com/contest/1324/problem/F
https://atcoder.jp/contests/dp/tasks/dp_v
https://mirror.codeforces.com/contest/960/problem/E
https://cses.fi/problemset/task/1132
https://cses.fi/problemset/task/1133
https://dmoj.ca/problem/dmopc14c4p6
https://oj.uz/problem/view/CEOI17_chase
https://dmoj.ca/problem/cco13p3
https://dmoj.ca/problem/coci08c1p6
Collected from various blogs. It might help.
This is quite a nice one!
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
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