当前位置:网站首页>Leetcode daily questions (2316. count unreachable pairs of nodes in an undirected graph)
Leetcode daily questions (2316. count unreachable pairs of nodes in an undirected graph)
2022-07-07 09:14:00 【wangjun861205】
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.
Return the number of pairs of different nodes that are unreachable from each other.
Example 1:
Input: n = 3, edges = [[0,1],[0,2],[1,2]]
Output: 0
Explanation: There are no pairs of nodes that are unreachable from each other. Therefore, we return 0.
Example 2:
Input: n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
Output: 14
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]].
Therefore, we return 14.
Constraints:
- 1 <= n <= 105
- 0 <= edges.length <= 2 * 105
- edges[i].length == 2
- 0 <= ai, bi < n
- ai != bi
- There are no repeated edges.
use union-find To turn every isolated graph into a tree , Then we can get the number of nodes in each tree , hypothesis nodes[i] Is the number of nodes in the current tree , that n - nodes[i] Is the number of nodes that are not connected to the nodes in the current tree , nodes[i] * (n - nodes[i]) Namely pair The number of , Traverse all the numbers to calculate pair The final answer is to add up the quantity
use std::collections::HashMap;
impl Solution {
fn find(parent: &mut Vec<i32>, n: i32) -> i32 {
let mut p = parent[n as usize];
if p == n {
return p;
}
p = Solution::find(parent, p);
parent[n as usize] = p;
p
}
fn union(parent: &mut Vec<i32>, a: i32, b: i32) {
let pa = Solution::find(parent, a);
let pb = Solution::find(parent, b);
parent[pa as usize] = pb;
}
pub fn count_pairs(n: i32, edges: Vec<Vec<i32>>) -> i64 {
let mut parent: Vec<i32> = (0..n).collect();
for e in edges {
Solution::union(&mut parent, e[0], e[1]);
}
while {
let mut modified = false;
for i in 0..n as usize {
let pp = parent[parent[i] as usize];
if parent[i] != pp {
Solution::union(&mut parent, i as i32, pp);
modified = true;
}
}
modified
} {
}
let m = parent.into_iter().fold(HashMap::new(), |mut m, v| {
*m.entry(v).or_insert(0) += 1;
m
});
let mut ans = 0;
for v in m.values() {
ans += *v * (n as i64 - *v);
}
ans / 2
}
}
边栏推荐
- Simulation volume leetcode [general] 1557 The minimum number of points that can reach all points
- Register address name mapping
- Skill review of test engineer before interview
- OpenGL三维图形绘制
- Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
- Serializer & modelserializer of DRF serialization and deserialization
- When inputting an expression in the input box, an error is reported: incorrect string value:'\xf0\x9f... ' for column 'XXX' at row 1
- What are the suggestions for PMP candidates?
- Calf problem
- NVIC中断优先级管理
猜你喜欢
随机推荐
[chaosblade: node disk filling, killing the specified process on the node, suspending the specified process on the node]
Synchronized underlying principle, volatile keyword analysis
Postman interface debugging method
Entity of cesium data visualization (Part 1)
[istio introduction, architecture, components]
Interpretation of MySQL optimization principle
Common operating commands of Linux
[chaosblade: delete pod according to the tag, pod domain name access exception scenario, pod file system i/o failure scenario]
Test Engineer Interview Questions 2022
Simulation volume leetcode [general] 1609 Parity tree
外部中断实现按键实验
Panel display technology: LCD and OLED
Leetcode刷题记录(数组)组合总和、组合总和 II
Selenium mouse sliding operation event
【ChaosBlade:根据标签删除POD、Pod 域名访问异常场景、Pod 文件系统 I/O 故障场景】
MySQL common statements
C language pointer (exercises)
硬件大熊原创合集(2022/06更新)
Locust performance test 3 (high concurrency, parameter correlation, assembly point)
Several common database connection methods