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 ?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 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 ?
Название |
---|
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