当前位置:网站首页>Leetcode counts the logarithm of points that cannot reach each other in an undirected graph
Leetcode counts the logarithm of points that cannot reach each other in an undirected graph
2022-06-29 02:33:00 【I am busy 2010】
Give you an integer n , Means a sheet of Undirected graph There is n Nodes , The number is 0 To n - 1 . And give you a two-dimensional array of integers edges , among edges[i] = [ai, bi] Representation node ai and bi There is a Undirected edge .
Please return Unable to reach each other Different Number of pairs .
Example 1:

Input :n = 3, edges = [[0,1],[0,2],[1,2]] Output :0 explain : All points can reach each other , It means that you can't reach each other without point pairs , So we go back to 0 .
Example 2:

Input :n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]] Output :14 explain : All in all 14 Point pairs cannot reach 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]] So we go back to 14 .
Tips :
1 <= n <= 1050 <= edges.length <= 2 * 105edges[i].length == 20 <= ai, bi < nai != bi- No duplicate edges .
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;
};边栏推荐
猜你喜欢

pvcreate asm disk导致asm磁盘组异常恢复---惜分飞
![[redis] sortedset type](/img/7f/f5f1aa603c8994b669d52a435fed7e.png)
[redis] sortedset type

三角函数计算

音响是如何把微弱声音放大呢

Prepare for the Blue Bridge Cup - double pointer, BFS

Programmers whose monthly salary is less than 30K must recite the interview stereotype. I'll eat it first

月薪没到30K的程序员必须要背的面试八股,我先啃为敬

What is the dry goods microservice architecture? What are the advantages and disadvantages?

SystemVerilog structure (I)

SystemVerilog-结构体(一)
随机推荐
table通过伪类实现 另类自适应
[redis] sortedset type
String length
Convert flat structure to tree structure
Koa quick start
How to use project Gantt chart to make project report
"The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express
110. simple chat room 13: chat room server
They all talk about interviews with big factories. When I interview with small factories, I invite people to drink tea?
Troubleshooting of pyinstaller failed to pack pikepdf
Studies of relative costs for development in different languages
字符串替换
Project R & D, what are the free brain mapping tools that are easy to use
The linkedhashset set makes the elements orderly without repetition
China's flexible employment has reached 200million
What is the Valentine's Day gift given by the operator to the product?
[redis] key hierarchy
Has Moore's law come to an end?
Ctfhub web password default password
Differences between web testing and app testing