Sponsored
Sponsored
This approach is based on using a Union-Find (Disjoint Set Union) data structure, which allows us to efficiently keep track of connected components in the graph. The strategy involves sorting both the edges
and the queries
based on the edge weights and the query limits, respectively. By processing these sorted lists, we can incrementally add edges to the Union-Find structure and query the connectivity status effectively.
Time Complexity: O(E log E + Q log Q + (E + Q) α(n))
Space Complexity: O(n)
1using System;
2using System.Linq;
3
4public class UnionFind {
5 private int[] parent;
6 private int[] rank;
7
8 public UnionFind(int size) {
9 parent = new int[size];
10 rank = new int[size];
11 for (int i = 0; i < size; i++) {
12 parent[i] = i;
13 rank[i] = 1;
14 }
15 }
16
17 public int Find(int u) {
18 if (u != parent[u]) {
19 parent[u] = Find(parent[u]);
20 }
21 return parent[u];
22 }
23
24 public void Union(int u, int v) {
25 int rootU = Find(u);
26 int rootV = Find(v);
27 if (rootU != rootV) {
28 if (rank[rootU] > rank[rootV]) {
29 parent[rootV] = rootU;
30 } else if (rank[rootU] < rank[rootV]) {
31 parent[rootU] = rootV;
32 } else {
33 parent[rootV] = rootU;
34 rank[rootU]++;
35 }
36 }
37 }
38
39 public bool Connected(int u, int v) {
40 return Find(u) == Find(v);
41 }
42}
43
44public class Solution {
45 public bool[] EdgeLengthLimitedPaths(int n, int[][] edgeList, int[][] queries) {
46 var uf = new UnionFind(n);
47 bool[] answers = new bool[queries.Length];
48 Array.Sort(edgeList, (a, b) => a[2].CompareTo(b[2]));
49
50 var indexedQueries = queries.Select((q, index) => new { q, index }).OrderBy(x => x.q[2]).ToArray();
51
52 int edgeIndex = 0;
53 foreach (var item in indexedQueries) {
54 int p = item.q[0], q = item.q[1], limit = item.q[2], qIndex = item.index;
55 while (edgeIndex < edgeList.Length && edgeList[edgeIndex][2] < limit) {
56 uf.Union(edgeList[edgeIndex][0], edgeList[edgeIndex][1]);
57 edgeIndex++;
58 }
59 answers[qIndex] = uf.Connected(p, q);
60 }
61
62 return answers;
63 }
64}
The C# solution uses a similar approach with Union-Find to efficiently handle path inquiries. Edges and queries are sorted by weight constraints, and queries check for connectivity using updated Union-Find based on the processed edges.
This approach involves modifying Kruskal's Minimum Spanning Tree algorithm to answer multiple queries about path existence. We leverage the intrinsic nature of Kruskal's algorithm which builds an MST (or a union-find structured graph) incrementally. For each query sorted by limit, we add edges with weight less than the limit to the union-find structure and check if two nodes are connected.
Time Complexity: O(E log E + Q log Q + (E + Q) α(n))
Space Complexity: O(n)
1#include <algorithm>
#include <numeric>
class UnionFind {
public:
std::vector<int> parent, rank;
UnionFind(int size) : parent(size), rank(size, 1) {
std::iota(parent.begin(), parent.end(), 0);
}
int find(int u) {
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
void union_sets(int u, int v) {
int rootU = find(u);
int rootV = find(v);
if (rootU != rootV) {
if (rank[rootU] > rank[rootV])
parent[rootV] = rootU;
else if (rank[rootU] < rank[rootV])
parent[rootU] = rootV;
else {
parent[rootV] = rootU;
++rank[rootU];
}
}
}
bool connected(int u, int v) {
return find(u) == find(v);
}
};
std::vector<bool> edgeLengthLimitedPathsKruskal(int n, std::vector<std::vector<int>>& edgeList, std::vector<std::vector<int>>& queries) {
UnionFind uf(n);
std::vector<bool> answers(queries.size(), false);
std::sort(edgeList.begin(), edgeList.end(), [](const std::vector<int>& a, const std::vector<int>& b) { return a[2] < b[2]; });
std::vector<std::pair<int, std::vector<int>>> sorted_queries;
for (int i = 0; i < queries.size(); ++i) {
sorted_queries.emplace_back(i, queries[i]);
}
std::sort(sorted_queries.begin(), sorted_queries.end(), [](const std::pair<int, std::vector<int>>& a, const std::pair<int, std::vector<int>>& b) { return a.second[2] < b.second[2]; });
int edgeIndex = 0;
for (const auto& [qIndex, qry] : sorted_queries) {
int p = qry[0], q = qry[1], limit = qry[2];
while (edgeIndex < edgeList.size() && edgeList[edgeIndex][2] < limit) {
uf.union_sets(edgeList[edgeIndex][0], edgeList[edgeIndex][1]);
++edgeIndex;
}
answers[qIndex] = uf.connected(p, q);
}
return answers;
}
Using C++, this solution modifies Kruskal's algorithm, concurrently managing component union information while sorting through the graph's edges and query constraints. It ensures each query's conditions are met before assessing component connectivity.