|
|
back to boardAny hints? My thoughts for now: - Generate list of parents for every node, as well as weight sum (2nd parent, 4th parent, 8th parent and so on). This will allow to calculate weight sum between two nodes in O(log N) - Implement LCA (better with O(1) time?) - Store root of subtree in variable - When new node comes, find LCA(new node, subtree root). 2 variants here: 1) LCA = new node (need to add sum of weights from new subtree root to previous subtree root) 2) LCA = existing subtree root. What to do here? - When we remove node? Re: Any hints? Sort v_i by depth-first order. d[v_i] is the sum of weights from the root to v_i The answer, when there are n nodes, is: sum_{i=1}^{n} d[v_i] - d[LCA(v_i, v_{i+1})]; where v_{n+1}:=v_1 Implement these operations using std::set with a custom comparator that orders vertices using depth-first order. The proof of the formula is left to the reader as an exercise. Re: Any hints? WOW, that's really smart. Thank you |
|
|