# Graph Weight Queries - Google Top Interview Questions

### Problem Statement :

```You are given two two-dimensional list of integers edges and queries.

edges represents an undirected graph and each element is in the form [x, y, w] meaning that vertices x and y are connected with edge weight w.

queries is also in the form [x, y, w] and represents the question of does there exist a path between x and y such that each edge in it have weight of at most w.

Return the number of queries that are true.

Constraints

n ≤ 100,000 where n is the length of edges

m ≤ 100,000 where m is the length of queries

Example 1

Input

edges = [

[0, 1, 5],

[1, 2, 6],

[2, 3, 7],

[0, 3, 4]

]

queries = [

[0, 3, 5],

[1, 0, 3]

]

Output

1

Explanation

We can go from 0 to 3 by following this path [0, 3] and the edge's weight is at most 5. There's no path from 1 to 0 where each edge weight is at most 3```

### Solution :

```                        ```Solution in C++ :

const int MAXN = 100005;
const int MAXD = 17;

// begin union find
int par[MAXN];
int find(int x) {
return par[x] == x ? x : (par[x] = find(par[x]));
}
bool merge(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return false;
}
par[x] = y;
return true;
}
// end union find

// begin structure for MST

vector<pair<int, int>> mstedges[MAXN];  // <destination vertex, weight of edge>
int ancestor[MAXN][MAXD];  // ancestor[i][d] is the node that is 2^d above i in the spanning tree
int ancestorweight[MAXN][MAXD];  // ancestorweight[i][d] is the maximum weight edge over the 2^d
// edges going up from i
int depth[MAXN];                 // depth[i] is the depth of i in its spanning tree

// returns the weight of the heaviest edge of the path from a to b
int getHeaviestEdgeWeight(int a, int b) {
assert(find(a) == find(b));
if (depth[a] < depth[b]) {
swap(a, b);
}
int ret = 0;
for (int d = MAXD - 1; d >= 0; d--) {
if (depth[a] - depth[b] >= (1 << d)) {
ret = max(ret, ancestorweight[a][d]);
a = ancestor[a][d];
}
}
assert(depth[a] == depth[b]);
for (int d = MAXD - 1; d >= 0; d--) {
if (ancestor[a][d] != ancestor[b][d]) {
ret = max(ret, ancestorweight[a][d]);
ret = max(ret, ancestorweight[b][d]);
a = ancestor[a][d];
b = ancestor[b][d];
}
}
if (a != b) {
ret = max(ret, ancestorweight[a][0]);
ret = max(ret, ancestorweight[b][0]);
a = ancestor[a][0];
b = ancestor[b][0];
}
return ret;
}

void initlca(int maxv) {
for (int d = 1; d < MAXD; d++) {
for (int i = 0; i <= maxv; i++) {
ancestor[i][d] = ancestor[ancestor[i][d - 1]][d - 1];
ancestorweight[i][d] =
max(ancestorweight[i][d - 1], ancestorweight[ancestor[i][d - 1]][d - 1]);
}
}
}

void dfs(int curr, int par) {
for (auto edge : mstedges[curr]) {
if (edge.first == par) {
continue;
}
depth[edge.first] = depth[curr] + 1;
ancestor[edge.first][0] = curr;
ancestorweight[edge.first][0] = edge.second;
dfs(edge.first, curr);
}
}
// end structure for MST

// sorts edges in increasing order of weight
bool edgesort(vector<int>& a, vector<int>& b) {
return a.back() < b.back();
}

int solve(vector<vector<int>>& edges, vector<vector<int>>& queries) {
sort(edges.begin(), edges.end(), edgesort);
int maxv = 0;
for (auto& out : edges) {
maxv = max(maxv, max(out[0], out[1]));
}
for (int i = 0; i <= maxv; i++) {
par[i] = i;
}
for (int i = 0; i <= maxv; i++) {
mstedges[i].clear();
}
for (auto& edge : edges) {
if (merge(edge[0], edge[1])) {
mstedges[edge[0]].emplace_back(edge[1], edge[2]);
mstedges[edge[1]].emplace_back(edge[0], edge[2]);
}
}
for (int i = 0; i <= maxv; i++) {
depth[i] = -1;
}
for (int i = 0; i <= maxv; i++) {
if (depth[i] == -1) {
depth[i] = 0;
ancestor[i][0] = i;
ancestor[i][0] = 0;
dfs(i, -1);
}
}
initlca(maxv);
int ret = 0;
for (auto& query : queries) {
ret += find(query[0]) == find(query[1]) &&
query[2] >= getHeaviestEdgeWeight(query[0], query[1]);
}
return ret;
}```
```

```                        ```Solution in Java :

import java.util.*;

/**
* This is a implementation class of union by rank(union by weight) with path compression.
*/
class DisjointSetUnion {
List<Integer> disJointSet;

public DisjointSetUnion(int n) {
disJointSet = new ArrayList<Integer>();
for (int i = 0; i < n; ++i) {
}
}

public void union(int firstNode, int secondNode) {
int firstNodeParent = find(firstNode);
int secondNodeParent = find(secondNode);
if (firstNodeParent == secondNodeParent)
return;
if (Math.abs(disJointSet.get(firstNodeParent))
>= Math.abs(disJointSet.get(secondNodeParent))) {
disJointSet.set(firstNodeParent,
disJointSet.get(firstNodeParent) + disJointSet.get(secondNodeParent));
disJointSet.set(secondNodeParent, firstNodeParent);
} else {
disJointSet.set(secondNodeParent,
disJointSet.get(secondNodeParent) + disJointSet.get(firstNodeParent));
disJointSet.set(firstNodeParent, secondNodeParent);
}
}

public int find(int node) {
if (disJointSet.get(node) < 0) {
return node;
}
int parent = find(disJointSet.get(node));
disJointSet.set(node, parent);
return parent;
}

public void printDisjointSet() {
disJointSet.forEach(element -> System.out.print(element + " "));
}
}

class Edge implements Comparable {
private int u, v, w;

public Edge(int u, int v, int w) {
this.u = u;
this.v = v;
this.w = w;
}

public int getU() {
return u;
}

public int getV() {
return v;
}

public int getW() {
return w;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Edge edge = (Edge) o;
return u == edge.u && v == edge.v && w == edge.w;
}

@Override
public int hashCode() {
return Objects.hash(u, v, w);
}

@Override
public int compareTo(Object o) {
Edge node = (Edge) o;
return this.w - node.getW();
}
}

class Solution {
public int solve(int[][] e, int[][] query) {
int noOfVertices = Integer.MIN_VALUE;
int n = e.length;
int q = query.length;
List<Edge> edges = new ArrayList<>();
for (int i = 0; i < n; ++i) {
int u = e[i][0];
int v = e[i][1];
int w = e[i][2];
noOfVertices = Math.max(noOfVertices, Math.max(u, v));
Edge edge = new Edge(u, v, w);
}
List<Edge> queries = new ArrayList<>();
for (int i = 0; i < q; ++i) {
int u = query[i][0];
int v = query[i][1];
int w = query[i][2];
Edge edge = new Edge(u, v, w);
}
Collections.sort(queries);
Collections.sort(edges);
int edgesPosition = 0;
int queriesPosition = 0;
DisjointSetUnion disjointSetUnion = new DisjointSetUnion(noOfVertices + 1);
while (queriesPosition < queries.size()) {
int val = queries.get(queriesPosition).getW();
edgesPosition = eddEdgesUptillValue(disjointSetUnion, edges, edgesPosition, val);
if (disjointSetUnion.find(queries.get(queriesPosition).getU())
== disjointSetUnion.find(queries.get(queriesPosition).getV())) {
}
++queriesPosition;
}
}
private int eddEdgesUptillValue(
DisjointSetUnion disjointSetUnion, List<Edge> edges, int edgesPosition, int val) {
while (edgesPosition < edges.size() && edges.get(edgesPosition).getW() <= val) {
disjointSetUnion.union(
edges.get(edgesPosition).getU(), edges.get(edgesPosition).getV());
++edgesPosition;
}
return edgesPosition;
}
}```
```

```                        ```Solution in Python :

class DSU:
def __init__(self, N):
self.par = list(range(N))
self.sz = [1] * N

def find(self, x):
if self.par[x] != x:
self.par[x] = self.find(self.par[x])
return self.par[x]

def union(self, x, y):
xr, yr = self.find(x), self.find(y)
if xr == yr:
return False
if self.sz[xr] < self.sz[yr]:
xr, yr = yr, xr
self.par[yr] = xr
self.sz[xr] += self.sz[yr]
self.sz[yr] = self.sz[xr]
return True

class Solution:
def solve(self, edges, queries):
N = max(max(u, v) for u, v, w in edges) + 1
edges.sort(key=itemgetter(2))
queries.sort(key=itemgetter(2))

dsu = DSU(N)
ans = i = 0
for u, v, w in queries:
while i < len(edges) and edges[i][2] <= w:
dsu.union(*edges[i][:2])
i += 1
if dsu.find(u) == dsu.find(v):
ans += 1

return ans```
```

## Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

## Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

## Heavy Light White Falcon

Our lazy white falcon finally decided to learn heavy-light decomposition. Her teacher gave an assignment for her to practice this new technique. Please help her by solving this problem. You are given a tree with N nodes and each node's value is initially 0. The problem asks you to operate the following two types of queries: "1 u x" assign x to the value of the node . "2 u v" print the maxim

## Number Game on a Tree

Andy and Lily love playing games with numbers and trees. Today they have a tree consisting of n nodes and n -1 edges. Each edge i has an integer weight, wi. Before the game starts, Andy chooses an unordered pair of distinct nodes, ( u , v ), and uses all the edge weights present on the unique path from node u to node v to construct a list of numbers. For example, in the diagram below, Andy

## Heavy Light 2 White Falcon

White Falcon was amazed by what she can do with heavy-light decomposition on trees. As a resut, she wants to improve her expertise on heavy-light decomposition. Her teacher gave her an another assignment which requires path updates. As always, White Falcon needs your help with the assignment. You are given a tree with N nodes and each node's value Vi is initially 0. Let's denote the path fr

## Library Query

A giant library has just been inaugurated this week. It can be modeled as a sequence of N consecutive shelves with each shelf having some number of books. Now, being the geek that you are, you thought of the following two queries which can be performed on these shelves. Change the number of books in one of the shelves. Obtain the number of books on the shelf having the kth rank within the ra