Can someone tell me how to solve this problem ALIENINV
Thanks!
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
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 |
Название |
---|
When you ask for help with a problem, say what you already came up with (more instructions here).
A good first step might be to compute the answer for every leaf. Take a leaf $$$L$$$ and root the tree there. For every other vertex $$$V$$$, it will be removed before $$$L$$$ if and only if the whole subtree of $$$V$$$ has values smaller than the value in $$$L$$$. Let $$$s[V]$$$ be the size of the subtree of $$$V$$$. Then, we want $$$L$$$ to have the smallest value among $$$s[V]+1$$$ vertices (that subtree and vertex $$$L$$$ itself), so the probability of $$$L$$$ being the smallest is $$$\frac{1}{s[V]+1}$$$. Since this is the probability of $$$V$$$ being before $$$L$$$, you can sum this up to get the expected value of the place of $$$L$$$ — because with that probability the place of $$$L$$$ is increased by $$$1$$$.
This is $$$O(N)$$$ solution to find an answer for a single leaf. I don't know how hard it is to apply it for non-leaf vertices. Try it and let us know.
Thanks a lot for your help. I'll keep in mind about the instructions next time. And do you mean $$$L$$$ should have the largest value than the whole subtree of $$$V$$$? or am I missing something?
Thanks!
Yup, the largest.