当前位置:网站首页>leetcode 统计无向图中无法互相到达点对数
leetcode 统计无向图中无法互相到达点对数
2022-06-29 02:29:00 【我很忙2010】
给你一个整数 n ,表示一张 无向图 中有 n 个节点,编号为 0 到 n - 1 。同时给你一个二维整数数组 edges ,其中 edges[i] = [ai, bi] 表示节点 ai 和 bi 之间有一条 无向 边。
请你返回 无法互相到达 的不同 点对数目 。
示例 1:

输入:n = 3, edges = [[0,1],[0,2],[1,2]] 输出:0 解释:所有点都能互相到达,意味着没有点对无法互相到达,所以我们返回 0 。
示例 2:

输入:n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]] 输出:14 解释:总共有 14 个点对互相无法到达: [[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]] 所以我们返回 14 。
提示:
1 <= n <= 1050 <= edges.length <= 2 * 105edges[i].length == 20 <= ai, bi < nai != bi- 不会有重复边。
C++
class Solution {
public:
int find(int x) {
if(pre[x]==x) {
return x;
}
int y=pre[x];
int root=find(y);
pre[x]=root;
return root;
}
long long countPairs(int n, vector<vector<int>>& edges) {
pre.resize(n);
for(int i=0;i<n;i++) {
pre[i]=i;
}
for(auto e:edges) {
int x=e[0];
int y=e[1];
int root1=find(x);
int root2=find(y);
if(root1!=root2) {
pre[root2]=root1;
}
}
unordered_map<int,long> mp;
for(int i=0;i<n;i++) {
int root=find(i);
mp[root]++;
}
long res=(long)n*(long)(n-1)/2;
for(auto it:mp) {
res-=it.second*(it.second-1)/2;
}
return res;
}
private:
vector<int> pre;
};边栏推荐
- 矩阵特征值和特征向量求解——特征值分解(EVD)
- 高并发的理解与设计方案
- 东方财富股票开户是会有什么风险吗?东方财富开户安全吗
- [redis] sortedset type
- [MySQL practice of high concurrency, high performance and high availability of massive data -9] - transaction concurrency control solutions lbcc and mvcc
- String substitution
- Junior final exam
- To apply for a test engineer after years, the resume with high scores should be written like this
- [network communication learning notes] socket IO setup and deployment
- Cross border information station
猜你喜欢
![[MySQL practice of high concurrency, high performance and high availability of massive data -9] - transaction concurrency control solutions lbcc and mvcc](/img/62/77c2274db4f92ad1d88901e149251c.jpg)
[MySQL practice of high concurrency, high performance and high availability of massive data -9] - transaction concurrency control solutions lbcc and mvcc

pvcreate asm disk导致asm磁盘组异常恢复---惜分飞

Day10 enumeration class and annotation

OpenResty 使用介绍

Differences between web testing and app testing

What is the Valentine's Day gift given by the operator to the product?
![[redis] data introduction & General Command & string type](/img/86/3abc5047f9c0a051f432e82ccc816c.png)
[redis] data introduction & General Command & string type

Oracle Recovery Tools实战批量坏块修复

学习太极创客 — MQTT 第二章(九)本章测试

chrome浏览器关闭更新弹窗
随机推荐
Introduction to openresty
大智慧手机股票开户哪个券商更安全更方便?
MySQL queries the data of today, yesterday, this week, last week, this month, last month, this quarter, last quarter, this year, last year
字符串输出
Quelques tests pour compléter l'environnement wasm
Which brokerage is safer and more convenient to open a stock account for big smart phones?
B1006 output integer in another format
Is there any risk in opening an account for Dongfang fortune stock? Is it safe for Dongfang fortune to open an account
字符串属性练习
【Redis】初识 Redis
矩阵特征值和特征向量求解——特征值分解(EVD)
How to use project Gantt chart to make project report
Redis master-slave replication
【网络通信学习笔记】Socket.IO的搭建和部署
SAP ui5 beginner tutorial 22 - development and use of filter
[redis] key hierarchy
[network communication learning notes] socket IO setup and deployment
[redis] hash type
Temperature conversion II
[untitled]