| Show all threads Hide all threads Show all messages Hide all messages |
| Good problem! | Felix_Mate | 1752. Tree 2 | 30 May 2026 14:16 | 6 |
I got AC with O(N*sqrt(N)),but my solution is very hard(274 lines and many different subtasks). Who can say simple solution(idea without code)? Hi. This is my solution for this problem. Let's find two farthest nodes in tree. Let's call it f1 and f2. It can be done by two bfs'. Now let's answer for queries. Let the y farthest node from x(it's f1 or f2). If such node exists that dist(x, z) = d, then it would lie on path from x to y. If dist(x, y) < d, answer would be 0. Let l be lca(x, y). If dist(l, x) <= d, answer would be dist(l, x)'th parent of x. If x is ancestor of y, answer would be (dist(x, y) - d)'th parent of y. Otherwise answer would be dist(l, y) - (d - dist(l, x)). Sorry for my poor English. O(N*log(N)), ~100 lines of code, ~1K gzipped For every edge out of a node we calculate L, the length of the longest path from this node thru the edge. It is two DFS: the first one to calculate L for downward edges, the second one to fix the upward edges. At most two edges (with the largest L) of each node are needed to calculate the longest paths (the second is needed for the situations where we get from node A into node B and in node B the edge B->A has the maximal L). For these two edges we precalculate short-cuts: for all i, such that 2^i β€ L, we save the node C that can be reached after 2^i steps and from which edge in C it was reached. My solution is quite simple, just find one farthest nodes pair in the tree by two DFSs, denoting f1 and f2. Then use f1 as root and dfs to calculate the 2^i steps' father of every node x. And use f2 as root similarly. Then the answer can be done by express d to the binary form and jump at most log(n) to find an answer under these two roots. We can actually solve it on just O(N) time. Let's set vertex 1 as a root. Now let's at first find the depths of subtree of each vertex, deepest vertex in each subtree and the distance from the root for each vertex. This can be done in one DFS. Let's name them as: 1) depth[ver] -> depth of a subtree of vertex ver 2) dist[ver] -> distance from vertex 1 to vertex ver 3) mx_ver[ver] -> deepest vertex in a subtree of vertex ver Now we can check for all queries: 1) If query is (v, d) and depth of vertex v is >= d, then the answer is the parent of vertex mx_ver[v] on distance (depth[v] - d) (so we should somehow later go up from vertex v to (depth[v] - d) edges). Let's store it in mx_ver[v]. 2) If query is (v, d) and distance from root to v >= d (so dst[v] >= d), then the answer is the parent of vertex v on distance d, let's store this information in vertex v. What should we do with all the other queries? All other queries (v, d) tell us that the answer for them will look somehow like this: Take vertex v, go up several edges, then go down to some other subtree. So we need to take some ancestor of v and go into it to a subtree different from a subtree containing vertex v. Let's say that this ancestor is some vertex u and the depth of its deepest subtree not containing v is H. Then if (dst[v] - dst[u] + H) >= d, we can choose u as a suitable ancestor. And the answer for the query will be the parent of the deepest vertex in subtree H on distance d - (dst[v] - dst[u] + H). Great thing about these formulas is that if we stay in some vertex v during some DFS, for all our ancestors the number (H - dst[u]) is a constant and we can keep the maximum value of it on current path. So what should we do next? Let's simply run one more DFS. During our walk through the tree in the DFS let's keep maximum value of (H - dst[u]) for all our ancestors. To do this we need to do some routine stuff: 1) Calculate two maximums by depth[to] + 1 for all children (if we will go to the subtree with first maximum, we will use H - dst from the second one). 2) Before going into some child check if the new found values are larger then global maximum (H - dst[u]). 3) Not forget to carefully restore this global maximum going up on each step. 4) Each time we take new maximum for (H - dst[u]), also store the deepest vertex in the subtree H. Then in each vertex during this DFS we also need to iterate through all remaining queries for this vertex and try to set an answer to them, again in form "Answer for this query is a parent of some deepest vertex in some subtree H on distance (dst[v] + (H - dst[u])) - d" The final step would be to run one more DFS and in all vertices with stored info "go up to distance X" do this by simply storing current DFS path and writing needed parent as an answer for the corresponding query. |
| why i have Runtime error (stack overflow) with Visual C++ x64, but AC with G++ 9.2? | >>> | 1752. Tree 2 | 30 May 2026 14:13 | 2 |
Why is there such a big difference between these two compilers and how do I know if I have solved this problem, it just feels like I wrote really dirty code and it somehow worked. #pragma comment(linker, "/STACK:16777216") |
| WA #23 | π¨π»βπ» Spatarel Dan Constantin | 1616. Square Country 4 | 29 May 2026 23:58 | 1 |
WA #23 π¨π»βπ» Spatarel Dan Constantin 29 May 2026 23:58 In test 23 the alpha angle is very close to 0 or 90. |
| If WA#6 | Timur Sitdikov (MSU Tashkent) | 1637. Triangle Game 2 | 29 May 2026 13:48 | 2 |
If WA#6 Timur Sitdikov (MSU Tashkent) 8 Jul 2011 11:43 Try this: 0 0 1 0 0 2 0 0 -1 0 0 -2 2 Re: If WA#6 π¨π»βπ» Spatarel Dan Constantin 29 May 2026 13:48 Input: 1 1 5 1 4 2 -1 -1 3 -1 2 -2 Output: 3 |
| Hint | Dmi3Molodov | 1868. Prediction Contest | 28 May 2026 21:15 | 1 |
Hint Dmi3Molodov 28 May 2026 21:15 solve the problem ONLY if you are VERY diligent. |
| N = 1 | coder | 2168. Runes in the Field | 28 May 2026 21:06 | 1 |
N = 1 coder 28 May 2026 21:06 What should be when N= 1 ? answer is 0 ? |
| Many many tests | andreyDagger`~ | 1955. Boss, I Can See You! | 28 May 2026 13:12 | 2 |
These tests didn't help me figure out the issue, but maybe they can be useful for community 7 0 0 0 2 1 1 1 3 3 1 0 -1 -1 0 3.95284707521047407042 6 0 1 1 2 0 2 -1 1 -1 0 1 0 2.82842712474619029095 7 2 0 2 3 -2 3 -2 0 -2 -2 0 -2 2 -2 6.40312423743284853117 3 0 0 1 0 0 1 1.41421356237309514547 8 1 2 1 3 2 2 2 1 1 0 1 1 0 1 0 2 3.00000000000000000000 12 2 0 1 1 0 1 1 0 0 -2 -1 1 -1 2 0 2 -1 3 -4 0 0 -4 0 -1 6.79869268479037932229 12 2 0 1 1 0 1 1 0 -2 0 -1 1 -1 2 0 2 -1 3 -4 0 -3 -1 0 -1 6.00000000000000000000 7 0 0 1 2 1 1 3 1 1 -1 1 -3 -1 0 5.00000000000000000000 16 3 0 4 1 3 1 3 2 2 2 2 1 1 1 1 2 0 2 0 1 -1 1 -1 2 -2 2 -2 1 -3 1 -2 0 7.00000000000000000000 Be careful! The test with the supposed answer 6.79869268479037932229 is invalid because it self-intersects! |
| WA 19 | π§ Vadim Barinov \Frez_Fstilus/'``' :) | 2112. Battle log | 27 May 2026 14:56 | 1 |
WA 19 π§ Vadim Barinov \Frez_Fstilus/'``' :) 27 May 2026 14:56 Test: 12 A B C D E F G H I J K L 12 A HIT B IN HEAD A REVIVE B C HIT D IN HEAD C REVIVE D D HIT E IN HEAD D REVIVE E F HIT G IN HEAD F REVIVE G G HIT H IN HEAD G REVIVE H I HIT J IN HEAD I REVIVE J Answer: CORRECT A B I J C D E K F G H L |
| Heavily incorrect russian statement | Igor Parfenov | 1288. Astrolocation | 27 May 2026 04:17 | 1 |
There are a lot of mistakes in russian statement. I don't think, that is makes sense to write all of them - just read the english statement. |
| WA #25 | π¨π»βπ» Spatarel Dan Constantin | 1271. Sailing Directions | 26 May 2026 17:33 | 1 |
WA #25 π¨π»βπ» Spatarel Dan Constantin 26 May 2026 17:33 Input: 100 100 0 10 10 10 0 0 19 11 1 10 10 20 10 20 20 Output: -1 |
| WA #2 | Rouk | 1304. Parallelepiped | 26 May 2026 03:51 | 3 |
WA #2 Rouk 19 Aug 2012 00:07 no point of S lies in G (but may lie on its side) |
| WA #3 | π¨π»βπ» Spatarel Dan Constantin | 1664. Pipeline Transportation | 25 May 2026 22:38 | 1 |
WA #3 π¨π»βπ» Spatarel Dan Constantin 25 May 2026 22:38 Input: 6 0 0 1 1 1 0 2 1 2 0 3 0 7 1 2 12 1 3 2 2 4 10 3 4 10 3 5 10 4 6 1 5 6 13 Output: 11 1 2 9 1 3 2 2 4 9 4 3 8 3 5 10 4 6 1 5 6 10 |
| To admins: New tests | Milanin | 1589. Sokoban | 25 May 2026 04:09 | 4 |
Hey admins, I've sent a couple of tests to timus_support@acm.timus.ru that my AC solution was struggling with. Please validate if they can be added to the system. Your tests have been added. Thanks! Milanin, thanks a lot for new tests! Now I've got AC only with neural networks solution. All solutions with just "optimizations"/"bad subfields & patterns", etc. didn't allow me to pass new tests. This is impressive. I still can't believe that there's a problem on Timus that has a meaningful neural network based solution. |
| WA #13 | π¨π»βπ» Spatarel Dan Constantin | 2050. 3D-modeling | 23 May 2026 15:19 | 1 |
WA #13 π¨π»βπ» Spatarel Dan Constantin 23 May 2026 15:19 Input: 0 0 0 0 0 1 0 1 0 0 1 -1 |
| Question | AlexF [USTU Frogs] | 1442. Floo Powder | 23 May 2026 01:37 | 2 |
Question AlexF [USTU Frogs] 7 Nov 2007 18:22 Please, tell me the volume of the smaller part of the cone in sample. Edited by author 07.11.2007 18:23 Re: Question π¨π»βπ» Spatarel Dan Constantin 23 May 2026 01:37 |
| Help! (+) | Dart MirzMan C++ Edition (Mirzoyan Alexey, Rybinsk SAAT) | 1566. Triangular Postcards | 22 May 2026 23:45 | 2 |
Help! (+) Dart MirzMan C++ Edition (Mirzoyan Alexey, Rybinsk SAAT) 9 Oct 2007 00:53 100 100 100 100 100 100 ? 200 100 100 100 50 50 ? Re: Help! (+) π¨π»βπ» Spatarel Dan Constantin 22 May 2026 23:45 The answer is YES to both examples. |
| overrated | ~'Yamca`~ | 1424. Minibus | 20 May 2026 22:25 | 1 |
|
| To Admins | π¨π»βπ» Spatarel Dan Constantin | 2216. Big Elevator | 19 May 2026 19:21 | 4 |
To Admins π¨π»βπ» Spatarel Dan Constantin 18 Mar 2026 10:40 Hi! I believe test #3 is invalid - there is no way to get from floor 1 to floor N. Can you please check? Thank you! Test is valid, but a bit unusual :) Try the boundary cases Re: To Admins π¨π»βπ» Spatarel Dan Constantin 19 Mar 2026 19:51 |
| Hint | Dmi3Molodov | 1450. Russian Pipelines | 19 May 2026 16:59 | 1 |
Hint Dmi3Molodov 19 May 2026 16:59 In Soviet times, several gas pipelines were sometimes built from A to B. Edited by author 19.05.2026 21:32 |
| WA #3 | π¨π»βπ» Spatarel Dan Constantin | 1477. Airplanes | 19 May 2026 13:53 | 1 |
WA #3 π¨π»βπ» Spatarel Dan Constantin 19 May 2026 13:53 Input: 2 0 0 1 0 1 0 Output: 2 |