Link to the problem can be found here.
Problem Statement
Burger Town is a city consisting of N special crossings and N-1 tracks. Between each pair there is exactly one short path intersection. The junction i is located at the point (xi, yi), and the distance between the two transitions i, j is determined by the geometry of the Taxicab.
Tim recently provided a taxi to work as a taxi driver. His car was very cheap, but it has a very big drawback. It can only control H units horizontally and V units vertically before refueling.
If the client wants to be brought from junction I to another j junction, then this car can only go along the route if the sum of the horizontal distances and the sum of the vertical distances to this path are less than or equal to H and V, respectively.
In addition, there is a unique path between any two transitions.
Now he has thoughts on returning the car back to the seller. But first, he wants to know, even if it's worth it. That's why he wants to know the number of unordered pairs (i, j), so that it is not possible to control the client from connection i to transition j.
Limitations
2 ≤ N ≤ 10 ^ 5
0 ≤ H, V ≤ 10 ^ 14
0 ≤ xi, yi ≤ 10 ^ 9
I solved this problem with recursion. But in some test cases, my code is disabled.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); long H = in.nextLong(); long V = in.nextLong(); List<Vertex> vertex = new ArrayList<>(); for (int i=0; i < N; i++) { Vertex vx = new Vertex(in.nextLong(), in.nextLong()); vertex.add(vx); } for (int i=0; i < N-1; i++) { int fromPath = in.nextInt()-1; int toPath = in.nextInt()-1; vertex.get(fromPath).neighbours.add(vertex.get(toPath)); vertex.get(toPath).neighbours.add(vertex.get(fromPath)); } long count = 0; for (int i=0; i < N; i++) { count += (N - findUnorderedPairs(vertex.get(i), null, 0, 0, H, V)); } System.out.println(count/2); int temp = 0; } private static long findUnorderedPairs(Vertex vertex, Vertex previousVertex, long hor, long vert, long H, long V) { if (hor > H || vert > V) { return 0; } long result = 1; for (Vertex v : vertex.neighbours) { result += (v != previousVertex) ? findUnorderedPairs(v, vertex, hor + Math.abs(vertex.x - vx), vert + Math.abs(vertex.y - vy), H, V) : 0; } return result; } private static class Vertex { private long x; private long y; public ArrayList<Vertex> neighbours; public Vertex(long x, long y) { this.x = x; this.y = y; neighbours = new ArrayList<>(); } } }
I also tried with the implementation of Dijkstras, but no luck there either.
Any suggestions on how to reach a faster solution would really be appreciated.