Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5e7e50f

Browse files
authored
Added task 2316.
1 parent6c36ac7 commit5e7e50f

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+
packageg2301_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+
importjava.util.HashMap;
6+
7+
publicclassSolution {
8+
publiclongcountPairs(intn,int[][]edges) {
9+
DSUd =newDSU(n);
10+
HashMap<Integer,Integer>map =newHashMap<>();
11+
for (int[]e :edges) {
12+
d.union(e[0],e[1]);
13+
}
14+
longans =0;
15+
for (inti =0;i <n;i++) {
16+
intp =d.findParent(i);
17+
intcnt =map.containsKey(p) ?map.get(p) :0;
18+
ans +=i -cnt;
19+
map.put(p,map.getOrDefault(p,0) +1);
20+
}
21+
returnans;
22+
}
23+
24+
privatestaticclassDSU {
25+
int[]rank;
26+
int[]parent;
27+
28+
DSU(intn) {
29+
rank =newint[n +1];
30+
parent =newint[n +1];
31+
for (inti =1;i <=n;i++) {
32+
parent[i] =i;
33+
}
34+
}
35+
36+
intfindParent(intnode) {
37+
if (parent[node] ==node) {
38+
returnnode;
39+
}
40+
parent[node] =findParent(parent[node]);
41+
returnfindParent(parent[node]);
42+
}
43+
44+
booleanunion(intx,inty) {
45+
intpx =findParent(x);
46+
intpy =findParent(y);
47+
if (px ==py) {
48+
returnfalse;
49+
}
50+
if (rank[px] >rank[py]) {
51+
parent[py] =px;
52+
}else {
53+
parent[px] =py;
54+
rank[py]++;
55+
}
56+
returntrue;
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
newThread(() ->foo.first(() ->fooData[0]++)).start();
1616
newThread(() ->foo.second(() ->fooData[0]++)).start();
1717
newThread(() ->foo.third(() ->fooData[0]++)).start();
18-
TimeUnit.MILLISECONDS.sleep(500);
18+
TimeUnit.MILLISECONDS.sleep(600);
1919
assertThat(fooData[0],equalTo(3));
2020
}
2121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packageg2301_2400.s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidcountPairs() {
11+
assertThat(newSolution().countPairs(3,newint[][] {{0,1}, {0,2}, {1,2}}),equalTo(0L));
12+
}
13+
14+
@Test
15+
voidcountPairs2() {
16+
assertThat(
17+
newSolution().countPairs(7,newint[][] {{0,2}, {0,5}, {2,4}, {1,6}, {5,4}}),
18+
equalTo(14L));
19+
}
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp