Skip to content

1697. Checking Existence of Edge Length Limited Paths 👍

  • Time: $O(\texttt{sort}(\texttt{queries}) + \texttt{sort}(\texttt{edgeList}))$
  • Space: $(q + n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class UnionFind {
 public:
  UnionFind(int n) : id(n), rank(n) {
    iota(id.begin(), id.end(), 0);
  }

  void unionByRank(int u, int v) {
    const int i = find(u);
    const int j = find(v);
    if (i == j)
      return;
    if (rank[i] < rank[j]) {
      id[i] = j;
    } else if (rank[i] > rank[j]) {
      id[j] = i;
    } else {
      id[i] = j;
      ++rank[j];
    }
  }

  int find(int u) {
    return id[u] == u ? u : id[u] = find(id[u]);
  }

 private:
  vector<int> id;
  vector<int> rank;
};

class Solution {
 public:
  vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList,
                                         vector<vector<int>>& queries) {
    vector<bool> ans(queries.size());
    UnionFind uf(n);

    for (int i = 0; i < queries.size(); ++i)
      queries[i].push_back(i);

    ranges::sort(queries, ranges::less{},
                 [](const vector<int>& query) { return query[2]; });
    ranges::sort(edgeList, ranges::less{},
                 [](const vector<int>& edge) { return edge[2]; });

    int i = 0;  // i := edgeList's index
    for (const vector<int>& query : queries) {
      const int p = query[0];
      const int q = query[1];
      const int limit = query[2];
      // Union edges whose distances < limit.
      while (i < edgeList.size() && edgeList[i][2] < limit)
        uf.unionByRank(edgeList[i][0], edgeList[i++][1]);
      if (uf.find(p) == uf.find(q))
        ans[query.back()] = true;
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class UnionFind {
  public UnionFind(int n) {
    id = new int[n];
    rank = new int[n];
    for (int i = 0; i < n; ++i)
      id[i] = i;
  }

  public void unionByRank(int u, int v) {
    final int i = find(u);
    final int j = find(v);
    if (i == j)
      return;
    if (rank[i] < rank[j]) {
      id[i] = j;
    } else if (rank[i] > rank[j]) {
      id[j] = i;
    } else {
      id[i] = j;
      ++rank[j];
    }
  }

  public int find(int u) {
    return id[u] == u ? u : (id[u] = find(id[u]));
  }

  private int[] id;
  private int[] rank;
}

class Solution {
  public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {
    boolean[] ans = new boolean[queries.length];
    int[][] qs = new int[queries.length][4];
    UnionFind uf = new UnionFind(n);

    for (int i = 0; i < queries.length; ++i) {
      qs[i][0] = queries[i][0];
      qs[i][1] = queries[i][1];
      qs[i][2] = queries[i][2];
      qs[i][3] = i;
    }

    Arrays.sort(qs, (a, b) -> Integer.compare(a[2], b[2]));
    Arrays.sort(edgeList, (a, b) -> Integer.compare(a[2], b[2]));

    int i = 0; // i := edgeList's index
    for (int[] q : qs) {
      // Union edges whose distances < limit (q[2]).
      while (i < edgeList.length && edgeList[i][2] < q[2])
        uf.unionByRank(edgeList[i][0], edgeList[i++][1]);
      if (uf.find(q[0]) == uf.find(q[1]))
        ans[q[3]] = true;
    }

    return ans;
  }
}