Recently I participated in google's coding exam and there are questions on Graph data structures, One of the question is that, they gives an undirected graph G with N nodes and M edges, He gives Q queries where in each query, he gives X Y W, where we have to check whether there is a path from X to Y with every edge must at most contain the weight <= W. So I tried storing the edges in adjacency list representation of graph and used the DFS method and visited array to check whether there is path following given constraints. It solved for partial test cases and not for private one's. So, I though it may be dense graph and I used Matrix representation of graph, it is showing memory limit exceeded. What should I do to solve these kind of problems?
Whenever I use matrix representation it gives memory limit exceeded and if I use Adjacency list representation, it gives time limit exceeded.Question Image
By the way, exam was completed few day back.
This is my first question. If I made any mistake please comment below