Hi
Can someone please describe the solution for the following problem which was asked in the quora haqathon 2014. Thanks in advance.
https://www.hackerrank.com/contests/quora-haqathon/challenges/relatedquestions
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Hi
Can someone please describe the solution for the following problem which was asked in the quora haqathon 2014. Thanks in advance.
https://www.hackerrank.com/contests/quora-haqathon/challenges/relatedquestions
Название |
---|
Related questions is one of standard problems "given a tree, you are not allowed to walk over some edge twice, calculate something" — and it can be done with DP. In first run you solve problem in which you are not allowed to move upward on tree — this is easy one; then you run dfs once again to solve remaining part (calculating EV for going up from some vertex — you may either go up from it's parent, or go down from it's parent to some other subtree; but once you go down — you'll move to previous problem, because remaining part of path will always move down, as moving up means using some edge twice).
I first tried to form a dp using (node,parent), which was O(n^2). Then I realized, every pair of (node,parent) is simply an edge. So I decided to enumerate all edges. Since it was a tree, there were n-1 edges. Since an edge can go both ways, I had to make them directed. And finally, there was this (node,-1). That is the initial node, which has no parent. There for I had a total of 3*n edges. So my DP was O(3*n). Yet, I only got 70 points :(. Remaining test cases got TLE. What was your exact complexity?
Strictly speaking, O(3*n) is O(n). Look for some bug in your code, or maybe your solution is not actually linear, or you have some problem like slow I/O... Hard to believe that linear solution for this problem gets TLE. I implemented it like I described — with 2 runs of dfs, and it works in 0.19.
Hi
Can u post your solution here? thanks.
Solution as mentioned by @I_love_Tanya_Romanova
I solved it using DP on tree and some quite complicated handling inside. Complexity O(N) (Using a hashtable, with a bit of effort you can use normal arrays) http://ideone.com/pPlbTB
The total number of states is O(Edges) which is O(N). And the loop inside which calls all children will never be called more than TWICE! We use these answers and sum them up to answer future calls to this node without doing this loop.
If you loop everytime, complexity can go up to O(N^2). My first solution had TLE because of this. Feel free to ask if you are having troubles analyzing the complexity of this solution...
Thanks for the code. It is very readable.
If i understand the algo correctly then exact complexity of the algo is o(N + 2*Edges). (As the algo makes sure that each edge is traversed at most and maybe exactly twice)
I re-coded it using the same logic to make sure i understand the algorithm. here is the link http://ideone.com/qma94w , but it does not pass all the test cases. can u help me with that? I appreciate the help, thanks.
Well you are correct for your complexity analysis but this applies to my code. Your code however can go up to O(N^2). Take for example a case like this one
What happens in this case? There are total of 2*Edges states as each edge can result in two different states. But what happens within each state? Can you see what happens each time the root vertex is called? It will iterate over ALL Edges! And all edges have one of their two states with the root as their node! So for each call to the root, it iterates over all edges and we have total of O(N) possible calls to the root so the complexity is O(N*N) which is O(N^2).
The way to work around this which I did was little complicated but you can find it in my code or you can think of it. Think of a way to get rid of this iteration which iterates over all the node edges.