2316. Count Unreachable Pairs of Nodes in an Undirected Graph Medium
1/**
2 * [2316] Count Unreachable Pairs of Nodes in an Undirected Graph
3 *
4 * 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 edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.
5 * Return the number of pairs of different nodes that are unreachable from each other.
6 *
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-3.png" style="width: 267px; height: 169px;" />
9 * Input: n = 3, edges = [[0,1],[0,2],[1,2]]
10 * Output: 0
11 * Explanation: There are no pairs of nodes that are unreachable from each other. Therefore, we return 0.
12 *
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-2.png" style="width: 295px; height: 269px;" />
15 * Input: n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
16 * Output: 14
17 * Explanation: There are 14 pairs of nodes that are unreachable from each other:
18 * [[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]].
19 * Therefore, we return 14.
20 *
21 *
22 * Constraints:
23 *
24 * 1 <= n <= 10^5
25 * 0 <= edges.length <= 2 * 10^5
26 * edges[i].length == 2
27 * 0 <= ai, bi < n
28 * ai != bi
29 * There are no repeated edges.
30 *
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/
35// discuss: https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39impl Solution {
40 pub fn count_pairs(n: i32, edges: Vec<Vec<i32>>) -> i64 {
41
42 }
43}
44
45// submission codes end
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn test_2316() {
53 }
54}
55
Back
© 2025 bowen.ge All Rights Reserved.