|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +
|
| 5 | +-* 2421. Number of Good Paths *- |
| 6 | +
|
| 7 | +
|
| 8 | +There is a tree (i.e. a connected, undirected graph with no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges. |
| 9 | +
|
| 10 | +You are given a 0-indexed integer array vals of length n where vals[i] denotes the value of the ith node. You are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi. |
| 11 | +
|
| 12 | +A good path is a simple path that satisfies the following conditions: |
| 13 | +
|
| 14 | +The starting node and the ending node have the same value. |
| 15 | +All nodes between the starting node and the ending node have values less than or equal to the starting node (i.e. the starting node's value should be the maximum value along the path). |
| 16 | +Return the number of distinct good paths. |
| 17 | +
|
| 18 | +Note that a path and its reverse are counted as the same path. For example, 0 -> 1 is considered to be the same as 1 -> 0. A single node is also considered as a valid path. |
| 19 | +
|
| 20 | + |
| 21 | +
|
| 22 | +Example 1: |
| 23 | +
|
| 24 | +
|
| 25 | +Input: vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] |
| 26 | +Output: 6 |
| 27 | +Explanation: There are 5 good paths consisting of a single node. |
| 28 | +There is 1 additional good path: 1 -> 0 -> 2 -> 4. |
| 29 | +(The reverse path 4 -> 2 -> 0 -> 1 is treated as the same as 1 -> 0 -> 2 -> 4.) |
| 30 | +Note that 0 -> 2 -> 3 is not a good path because vals[2] > vals[0]. |
| 31 | +Example 2: |
| 32 | +
|
| 33 | +
|
| 34 | +Input: vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] |
| 35 | +Output: 7 |
| 36 | +Explanation: There are 5 good paths consisting of a single node. |
| 37 | +There are 2 additional good paths: 0 -> 1 and 2 -> 3. |
| 38 | +Example 3: |
| 39 | +
|
| 40 | +
|
| 41 | +Input: vals = [1], edges = [] |
| 42 | +Output: 1 |
| 43 | +Explanation: The tree consists of only one node, so there is one good path. |
| 44 | + |
| 45 | +
|
| 46 | +Constraints: |
| 47 | +
|
| 48 | +n == vals.length |
| 49 | +1 <= n <= 3 * 104 |
| 50 | +0 <= vals[i] <= 105 |
| 51 | +edges.length == n - 1 |
| 52 | +edges[i].length == 2 |
| 53 | +0 <= ai, bi < n |
| 54 | +ai != bi |
| 55 | +edges represents a valid tree. |
| 56 | +
|
| 57 | +*/ |
| 58 | + |
| 59 | +import 'dart:math'; |
| 60 | + |
| 61 | +class A { |
| 62 | + late List<int> root; |
| 63 | + late List<int> cnt; |
| 64 | + int get(int x) { |
| 65 | + return x == root[x] ? x : (root[x] = get(root[x])); |
| 66 | + } |
| 67 | + |
| 68 | + int numberOfGoodPaths(List<int> vals, List<List<int>> edges) { |
| 69 | + // each node is a good path |
| 70 | + int n = vals.length, ans = n; |
| 71 | + cnt = List.filled(n, 0); |
| 72 | + root = List.filled(n, 0); |
| 73 | + // each element is in its own group initially |
| 74 | + for (int i = 0; i < n; i++) { |
| 75 | + root[i] = i; |
| 76 | + cnt[i] = 1; |
| 77 | + } |
| 78 | + // sort by vals |
| 79 | + List<List<int>> edgesList = []; |
| 80 | + for (int i = 0; i < edges.length; i++) edgesList.add(edges[i]); |
| 81 | + // Collections.sort(edgesList, new Comparator<int[]>() { |
| 82 | + // int compare(int[] x, int[] y) { |
| 83 | + // int a = Math.max(vals[x[0]], vals[x[1]]); |
| 84 | + // int b = Math.max(vals[y[0]], vals[y[1]]); |
| 85 | + // if(a < b) return -1; |
| 86 | + // else if(a > b) return 1; |
| 87 | + // else return 0; |
| 88 | + // } |
| 89 | + // }); |
| 90 | + |
| 91 | + edgesList.sort((List<int> x, List<int> y) { |
| 92 | + int a = max(vals[x[0]], vals[x[1]]); |
| 93 | + int b = max(vals[y[0]], vals[y[1]]); |
| 94 | + if (a < b) |
| 95 | + return -1; |
| 96 | + else if (a > b) |
| 97 | + return 1; |
| 98 | + else |
| 99 | + return 0; |
| 100 | + }); |
| 101 | + |
| 102 | + // iterate each edge |
| 103 | + for (List<int> e in edgesList) { |
| 104 | + int x = e[0], y = e[1]; |
| 105 | + // get the root of x |
| 106 | + x = get(x); |
| 107 | + // get the root of y |
| 108 | + y = get(y); |
| 109 | + // if their vals are same, |
| 110 | + if (vals[x] == vals[y]) { |
| 111 | + // then there would be cnt[x] * cnt[y] good paths |
| 112 | + ans += cnt[x] * cnt[y]; |
| 113 | + // unite them |
| 114 | + root[x] = y; |
| 115 | + // add the count of x to that of y |
| 116 | + cnt[y] += cnt[x]; |
| 117 | + } else if (vals[x] > vals[y]) { |
| 118 | + // unite them |
| 119 | + root[y] = x; |
| 120 | + } else { |
| 121 | + // unite them |
| 122 | + root[x] = y; |
| 123 | + } |
| 124 | + } |
| 125 | + return ans; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +class B { |
| 130 | + List<int> parents = []; |
| 131 | + int find(int x) { |
| 132 | + if (x != parents[x]) parents[x] = find(parents[x]); |
| 133 | + return parents[x]; |
| 134 | + } |
| 135 | + |
| 136 | + int numberOfGoodPaths(List<int> vals, List<List<int>> edges) { |
| 137 | + int n = vals.length; |
| 138 | + if (n == 1) return 1; |
| 139 | + // 1. init union find and ids arrayList |
| 140 | + parents = List.filled(n, 0); |
| 141 | + List<int> ids = []; |
| 142 | + for (int i = 0; i < n; i++) { |
| 143 | + parents[i] = i; |
| 144 | + ids.add(i); |
| 145 | + } |
| 146 | + |
| 147 | + // build Graph |
| 148 | + Map<int, Set<int>> graph = Map(); |
| 149 | + |
| 150 | + for (List<int> edge in edges) { |
| 151 | + int u = edge[0]; |
| 152 | + int v = edge[1]; |
| 153 | + |
| 154 | + graph.putIfAbsent(u, () => Set<int>()); |
| 155 | + graph.putIfAbsent(v, () => Set<int>()); |
| 156 | + |
| 157 | + graph[v]?.add(u); |
| 158 | + graph[u]?.add(v); |
| 159 | + } |
| 160 | + |
| 161 | + // 2. sort the ids by vals |
| 162 | + // Collections.sort(ids, (a, b) -> (vals[a] - vals[b])); |
| 163 | + ids.sort((a, b) => vals[a] - vals[b]); |
| 164 | + |
| 165 | + // 3. iterate from smallest to biggest |
| 166 | + int ret = 0; |
| 167 | + for (int i = 0; i < n; i++) { |
| 168 | + // get all same vals of node ids[i, j) |
| 169 | + int j = i + 1; |
| 170 | + while (j < n && vals[ids[j]] == vals[ids[i]]) j++; |
| 171 | + |
| 172 | + // enum each same val |
| 173 | + for (int k = i; k < j; k++) { |
| 174 | + int x = ids[k]; |
| 175 | + // traverse neighbor, if small than current node |
| 176 | + for (int neighbor in graph[x] ?? []) { |
| 177 | + if (vals[x] >= vals[neighbor]) { |
| 178 | + // union node x and its smaller neighbor |
| 179 | + parents[find(x)] = find(neighbor); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + // checkout for current values, the # of val in each component |
| 184 | + Map<int, int> temp = Map(); |
| 185 | + for (int k = i; k < j; k++) { |
| 186 | + int root = find(ids[k]); |
| 187 | + temp[root] = |
| 188 | + (temp[root] ?? 0) + 1; // # of current val in the {root} group |
| 189 | + } |
| 190 | + // standalone nodes are included. Note C(n, 2) + n = C(n + 1, 2) |
| 191 | + for (int v in temp.values) { |
| 192 | + ret += v * (v + 1) ~/ 2; |
| 193 | + } |
| 194 | + |
| 195 | + i = j - 1; |
| 196 | + } |
| 197 | + |
| 198 | + return ret; |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +class C { |
| 203 | + int numberOfGoodPaths(List<int> vals, List<List<int>> edges) { |
| 204 | + int n = vals.length; |
| 205 | + int sum = n; |
| 206 | + |
| 207 | + // create adjacency list |
| 208 | + List<List<int>> adjList = []; |
| 209 | + |
| 210 | + // create sortedmap with key as node value and value as list of index |
| 211 | + Map<int, List<int>> tm = Map(); |
| 212 | + |
| 213 | + // create unionfind |
| 214 | + UnionFind uf = UnionFind(n); |
| 215 | + |
| 216 | + for (int i = 0; i < n; i++) { |
| 217 | + //O(N) |
| 218 | + adjList.add([]); |
| 219 | + if (!tm.containsKey(vals[i])) { |
| 220 | + tm[vals[i]] = []; |
| 221 | + } |
| 222 | + tm[vals[i]]?.add(i); |
| 223 | + } |
| 224 | + for (List<int> edge in edges) { |
| 225 | + //O(N) |
| 226 | + adjList[edge[0]].add(edge[1]); |
| 227 | + adjList[edge[1]].add(edge[0]); |
| 228 | + } |
| 229 | + |
| 230 | + // traverse node values from lowest to highest |
| 231 | + MapEntry<int, List<int>> curr = tm.entries.first; |
| 232 | + while (curr != null) { |
| 233 | + List<int> listNodes = curr.value; |
| 234 | + |
| 235 | + // for each node union with neighbor if neighbor value is |
| 236 | + // lower or equal to node value |
| 237 | + for (int node in listNodes) { |
| 238 | + List<int> neighbors = adjList[node]; |
| 239 | + for (int neighbor in neighbors) { |
| 240 | + if (vals[node] >= vals[neighbor]) { |
| 241 | + uf.merge(node, neighbor); |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + // check if each node is in union with other node with |
| 247 | + // same value |
| 248 | + if (listNodes.length > 1) { |
| 249 | + Map<int, int> freq = Map(); |
| 250 | + |
| 251 | + // create frequency map of parent, to count number of nodes of same value in each set |
| 252 | + for (int i = 0; i < listNodes.length; i++) { |
| 253 | + int parent = uf.find(listNodes[i]); |
| 254 | + freq[parent] = (freq[parent] ?? 0) + 1; |
| 255 | + } |
| 256 | + |
| 257 | + // apply arithmetic progression formula to find sum of good paths |
| 258 | + for (int parentKey in freq.keys) { |
| 259 | + int frequency = freq[parentKey]! - 1; |
| 260 | + sum += (frequency * (frequency + 1)) ~/ 2; |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + curr = tm.entries.elementAt(curr.key); |
| 265 | + } |
| 266 | + return sum; |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +class UnionFind { |
| 271 | + late List<int> dp; |
| 272 | + late List<int> rank; |
| 273 | + |
| 274 | + UnionFind(int n) { |
| 275 | + dp = List.filled(n, 0); |
| 276 | + rank = List.filled(n, 0); |
| 277 | + for (int i = 0; i < n; i++) { |
| 278 | + dp[i] = i; |
| 279 | + rank[i] = 1; |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + int find(int i) { |
| 284 | + if (dp[i] == i) { |
| 285 | + return i; |
| 286 | + } |
| 287 | + dp[i] = find(dp[i]); |
| 288 | + return dp[i]; |
| 289 | + } |
| 290 | + |
| 291 | + bool isSameSet(int x, int y) { |
| 292 | + return find(x) == find(y); |
| 293 | + } |
| 294 | + |
| 295 | + void merge(int x, int y) { |
| 296 | + if (isSameSet(x, y)) { |
| 297 | + return; |
| 298 | + } |
| 299 | + int xp = find(x); |
| 300 | + int yp = find(y); |
| 301 | + |
| 302 | + if (rank[xp] < rank[yp]) { |
| 303 | + dp[xp] = yp; |
| 304 | + } else { |
| 305 | + dp[yp] = dp[xp]; |
| 306 | + if (rank[xp] == rank[yp]) { |
| 307 | + rank[xp]++; |
| 308 | + } |
| 309 | + } |
| 310 | + } |
| 311 | +} |
0 commit comments