I'm having some difficulties with this problems? Can you help me?
I think it can be solved by using Dynamic programming on tree, isn't it?
№ | Пользователь | Рейтинг |
---|---|---|
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 |
I'm having some difficulties with this problems? Can you help me?
I think it can be solved by using Dynamic programming on tree, isn't it?
Название |
---|
I will describe solution, which complexity seems to be , but it passed in 0.31 sec (I don't know how, maybe tests are very weak, maybe it will pass any test).
First, we have rooted tree, where root is vertex 1. Every vertex (except root) has a parent. We need to find subtree of out tree, which contains root, overall cost of conquering it is not more than M, and amount of oil in vertices is maximal from all situations where above conditions are satisfated.
First idea is that we can make edges [vertice — parent] rather vertices have a cost of conquering, it will not change answer (because every valid subtree, which contains root, covers all paths of type [vertice in subtree — root]. Let vertex 1 have 0 oil and cost of 0.
Now lets make problem recursion-friendy. Suppose we solve next problem : we can have any amount of money from 0 to M in budget, calculate for every 0 ≤ i ≤ M maximum amount of oil that we can conquer using not more, than i money.
If root is only vertex of tree then answer is obviously amount of oil in vertex, repeated from 0 to M. If our tree has at least one edge, we will delete one edge of type [root — vertice], calculate answers for rooted trees with roots in ends of edge we have cut. So now we can combine answer for full tree from answers for subtrees. It can be done in .
So now we can solve our problem by solving similar subproblems. There will be at most 2n - 1 recursive calls, because for every recursive call, except ones, which argument is tree with 0 edges, one edge is deleted. So runtime is . It still is too slow, but there is one optimisation, which makes solution much faster. You can look my code for it (it is commented, though maybe not completely clearly : 7663770 .
I hope HellKitsune can tell you better solution : he/she has solved this problem at about same time with less problems.
This is the same problem as the one discussed in http://codeforces.me/blog/entry/13168
I gave an O(nm) solution in the comments there.