当前位置:网站首页>LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
LeetCode每日一题(2316. Count Unreachable Pairs of Nodes in an Undirected Graph)
2022-07-07 06:28: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.
用 union-find 的方法来把每一个孤立的图变成一棵树, 然后我们可以得到每棵树里面的节点数量, 假设 nodes[i]是当前树的节点数量, 那 n - nodes[i]就是所有与当前树里的节点不相连的节点数量, nodes[i] * (n - nodes[i])就是 pair 的数量, 遍历所有的数计算出 pair 数量进行加和就是最终答案
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
}
}
边栏推荐
- Original collection of hardware bear (updated on June 2022)
- PMP Exam Preparation experience, seek common ground while reserving differences, and successfully pass the exam
- With an annual salary of 50W, Alibaba P8 will come out in person to teach you how to advance from testing
- Vagrant failed to mount directory mount: unknown filesystem type 'vboxsf'
- Simple use of Xray
- PMP examination experience sharing
- Postman interface test (I. installation and use)
- 硬件大熊原创合集(2022/06更新)
- How long does the PMP usually need to prepare for the exam in advance?
- MySQL master-slave delay solution
猜你喜欢

The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider

What are the conditions for applying for NPDP?

External interrupt to realize key experiment

寄存器地址名映射

Troublesome problem of image resizing when using typora to edit markdown to upload CSDN
![[istio introduction, architecture, components]](/img/2b/f84e5cdac6ed9b429e053ffc8dbeb0.png)
[istio introduction, architecture, components]

ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒

2022-07-06 unity core 9 - 3D animation

JVM 垃圾回收 详细学习笔记(二)

STM32 serial port register library function configuration method
随机推荐
Digital triangle model acwing 1027 Grid access
Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
Idea development environment installation
LeetCode 736. LISP syntax parsing
【ChaosBlade:节点 CPU 负载、节点网络延迟、节点网络丢包、节点域名访问异常】
JVM garbage collection detailed learning notes (II)
Calf problem
Test Engineer Interview Questions 2022
What are the suggestions for PMP candidates?
C语言指针(上篇)
C language pointer (Part 2)
C language pointer (exercises)
Simple use of Xray
C language pointer (Part 2)
C语言指针(特别篇)
Led analog and digital dimming
Implement custom memory allocator
STM32 serial port register library function configuration method
串口實驗——簡單數據收發
The longest ascending subsequence model acwing 1017 Strange thief Kidd's glider