I'm trying to solve it using BIT (each index is a color) and dfs, the output for node 1 is correct with me I'm trying to do rerooting technique on the tree to get the answer for the rest of nodes.
any idea ?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3821 |
3 | Benq | 3736 |
4 | Radewoosh | 3631 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3388 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
I'm trying to solve it using BIT (each index is a color) and dfs, the output for node 1 is correct with me I'm trying to do rerooting technique on the tree to get the answer for the rest of nodes.
any idea ?
Name |
---|
Rerooting doesn't apply because the problem already tells you node 1 is the root. The problem isn't asking what the answer would be if the tree were rooted at node $$$x$$$ for all $$$x$$$; it's asking what the answer for a subtree rooted at $$$x$$$, given that node 1 is the root.
You are right !!! Thanks for pointing out that
This problem requires knowledge of small-to-large set merging. Simply recurse from the leaves of the tree, and at each node merge all sets of its children to the set of its largest child. The time complexity of this is amortized $$$O(N \log N)$$$. Simple proof is as follows:
When you move an element from a set, the new set it belongs to will always have at least double the size of the previous set (since we always choose the largest one). Since the maximum size of any set is $$$O(N)$$$, each node will only be moved a maximum of $$$O(\log N)$$$ times.
Thanks a lot for you help :)
I think I recall seeing it here: https://cses.fi/book/book.pdf
This is one of the main applications of Mo's alogrithm on Trees. You can read about it here CF Blog for Mo's algo on Trees