dx24816's blog

By dx24816, history, 6 years ago, In English

Hello,

Can somebody help me debug my code for USACO newbarn (http://www.usaco.org/index.php?page=viewproblem2&cpid=817)? I am in dire need of help, as I have been debugging for around a week. I have no idea why my program is wrong, as every test case I give it, it gives me the right answer. I eventually gave up and just used the USACO test cases, but I it only gets the wrong answer by like line 1000, everything before is right. My strategy is as follows: I use DSU to find the components of all of the barns, and then put all the barns in each component into the forest vector. To calculate the maximum distance, I arbitrarily root each tree in forest, and DFS to precompute the depths of each node. Let a node x be in the subtree of a neighbor n of the root r. The maximum distance to another node is the maximum depth of any node in the subtrees of the neighbors of r (excluding the subtree that n and x are in), or it is the depth of x, or it is the maximum depth of any node in the subtree of n minus the depth of x. I reorder nodes using the seg map making sure nodes in the same component are consecutive, and put them into a segment tree. The bounds vector stores the left most and right most node in each component in the segment tree. Then for each query, I compute the maximum diameter, or update. Can somebody please find my error? Also, I am in need of debugging tips, as I often have the right idea, but can't implement it correctly. If you have any questions at all about my code, just ask. Thanks!

Code (https://ideone.com/oyX7GA)

Edit: It would also be much appreciated if someone found a test case I could test by hand where my program fails. The problem is that all 30 test cases I came up with were passed by my program.

-dx24816

  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

If you wish, I can explain my approach. Your approach seem to me too complicated.