VitSalis's blog

By VitSalis, 13 years ago, In English

Hello everyone. Recently I tried to learn LCA from the topcoder tutorial. I have solved 1 problem (spoj.com/problems/QTREE2) and now I am looking for some other beginers problems. Do you have any suggestions?

Tags lca
  • Vote: I like it
  • +4
  • Vote: I do not like it

| Write comment?
»
13 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
  • »
    »
    13 years ago, hide # ^ |
    ← Rev. 3  
    Vote: I like it 0 Vote: I do not like it

    I have been thinking abt 1752 and came up with an idea. And Im interested in if it is correct:

    1) using memoization and several sophisticated DFS I will determine for every (u,v) edge ((v,u) being different) longest vertex from u going through v, thereby easily calculating longest vertex from u. hope it is O(N).

    2) Then I will make parents array: DP[i][j] = 2^j-th parent from i. (N*Log(N))

    3) for every query, determine LCA in (logN) and if d>max(distance(u, LCA(u, longest_from_u)),distance(u, LCA(u, longest_from_u))) then gradually decrease distance between u and LCA(u, longest_from_u) until we reach wanted vertex. else Well if all above is correct there is not really a need to write any more :D.

    is it correct? : ))

    is there better solution?

    • »
      »
      »
      13 years ago, hide # ^ |
      ← Rev. 2  
      Vote: I like it +1 Vote: I do not like it

      Great spoiler is here

    • »
      »
      »
      13 years ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      I have a confusion about step 1.The longest distance from any to any other node may change when you change the root.think about a complete binary tree with 7 nodes considering 1 as root ((1),(2,3),(4,5,6,7)). the longest distance from any node found by dfs(1) call will return you at most 2 . But if you start dfs from a leaf node does everything remain same??? no, the other leafs are now at distance 4. Am I right?

      • »
        »
        »
        »
        13 years ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        I didnt understand what you said cuz Im stoned right now. But i can tell I solved using the spoiler mentioned by dalex.

»
13 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

this is just LCA problem LCA

»
13 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
»
8 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

https://www.spoj.com/problems/LCA/ this problem is based only on lca.