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 | 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'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