Skip to content

Commit 5e7e50f

Browse files
authored
Added task 2316.
1 parent 6c36ac7 commit 5e7e50f

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.11'
14851485

14861486
| # | Title | Difficulty | Tag | Time, ms | Time, %
14871487
|------|----------------|-------------|-------------|----------|---------
1488+
| 2316 |[Count Unreachable Pairs of Nodes in an Undirected Graph](src/main/java/g2301_2400/s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph/Solution.java)| Medium || 32 | 100.00
14881489
| 2315 |[Count Asterisks](src/main/java/g2301_2400/s2315_count_asterisks/Solution.java)| Easy || 1 | 100.00
14891490
| 2312 |[Selling Pieces of Wood](src/main/java/g2301_2400/s2312_selling_pieces_of_wood/Solution.java)| Hard | Backtracking | 78 | 63.64
14901491
| 2311 |[Longest Binary Subsequence Less Than or Equal to K](src/main/java/g2301_2400/s2311_longest_binary_subsequence_less_than_or_equal_to_k/Solution.java)| Medium | String, Dynamic_Programming, Greedy, Memoization | 1 | 100.00
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g2301_2400.s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph;
2+
3+
// #Medium #2022_06_26_Time_32_ms_(100.00%)_Space_108.9_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
7+
public class Solution {
8+
public long countPairs(int n, int[][] edges) {
9+
DSU d = new DSU(n);
10+
HashMap<Integer, Integer> map = new HashMap<>();
11+
for (int[] e : edges) {
12+
d.union(e[0], e[1]);
13+
}
14+
long ans = 0;
15+
for (int i = 0; i < n; i++) {
16+
int p = d.findParent(i);
17+
int cnt = map.containsKey(p) ? map.get(p) : 0;
18+
ans += i - cnt;
19+
map.put(p, map.getOrDefault(p, 0) + 1);
20+
}
21+
return ans;
22+
}
23+
24+
private static class DSU {
25+
int[] rank;
26+
int[] parent;
27+
28+
DSU(int n) {
29+
rank = new int[n + 1];
30+
parent = new int[n + 1];
31+
for (int i = 1; i <= n; i++) {
32+
parent[i] = i;
33+
}
34+
}
35+
36+
int findParent(int node) {
37+
if (parent[node] == node) {
38+
return node;
39+
}
40+
parent[node] = findParent(parent[node]);
41+
return findParent(parent[node]);
42+
}
43+
44+
boolean union(int x, int y) {
45+
int px = findParent(x);
46+
int py = findParent(y);
47+
if (px == py) {
48+
return false;
49+
}
50+
if (rank[px] > rank[py]) {
51+
parent[py] = px;
52+
} else {
53+
parent[px] = py;
54+
rank[py]++;
55+
}
56+
return true;
57+
}
58+
}
59+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2316\. Count Unreachable Pairs of Nodes in an Undirected Graph
2+
3+
Medium
4+
5+
You are given an integer `n`. There is an **undirected** graph with `n` nodes, numbered from `0` to `n - 1`. You are given a 2D integer array `edges` where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an **undirected** edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.
6+
7+
Return _the **number of pairs** of different nodes that are **unreachable** from each other_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2022/05/05/tc-3.png)
12+
13+
**Input:** n = 3, edges = [[0,1],[0,2],[1,2]]
14+
15+
**Output:** 0
16+
17+
**Explanation:** There are no pairs of nodes that are unreachable from each other.
18+
19+
Therefore, we return 0.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2022/05/05/tc-2.png)
24+
25+
**Input:** n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
26+
27+
**Output:** 14
28+
29+
**Explanation:** There are 14 pairs of nodes that are unreachable from each other: [[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]].
30+
31+
Therefore, we return 14.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= n <= 10<sup>5</sup></code>
36+
* <code>0 <= edges.length <= 2 * 10<sup>5</sup></code>
37+
* `edges[i].length == 2`
38+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
39+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
40+
* There are no repeated edges.

src/test/java/g1101_1200/s1114_print_in_order/FooTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void foo() throws InterruptedException {
1515
new Thread(() -> foo.first(() -> fooData[0]++)).start();
1616
new Thread(() -> foo.second(() -> fooData[0]++)).start();
1717
new Thread(() -> foo.third(() -> fooData[0]++)).start();
18-
TimeUnit.MILLISECONDS.sleep(500);
18+
TimeUnit.MILLISECONDS.sleep(600);
1919
assertThat(fooData[0], equalTo(3));
2020
}
2121
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2301_2400.s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countPairs() {
11+
assertThat(new Solution().countPairs(3, new int[][] {{0, 1}, {0, 2}, {1, 2}}), equalTo(0L));
12+
}
13+
14+
@Test
15+
void countPairs2() {
16+
assertThat(
17+
new Solution().countPairs(7, new int[][] {{0, 2}, {0, 5}, {2, 4}, {1, 6}, {5, 4}}),
18+
equalTo(14L));
19+
}
20+
}

0 commit comments

Comments
 (0)