323. Number of Connected Components in an Undirected Graph (leetcode)
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.
Example 1:
Input:n = 5andedges = [[0, 1], [1, 2], [3, 4]]0 3
| |
1 --- 2 4 Output: 2
Example 2:
Input:n = 5andedges = [[0, 1], [1, 2], [2, 3], [3, 4]]0 4
| |
1 --- 2 --- 3 Output: 1
Note:
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
idea: //use union find and count
class Solution {
class UnionFind{
HashMap<Integer,Integer> map = new HashMap<>();
HashMap<Integer,Integer> sz = new HashMap<>();
int count;//num of component
UnionFind(int n){
count = n;//?
for(int i = 0; i<n; i++){
map.put(i, i);
sz.put(i, 1);
}
}
Integer root(int p){
if(!map.containsKey(p)) return null;
while(p != map.get(p)){
Integer temp = map.get(map.get((p)));
map.put(p, temp);
p = map.get(p);
}
return p;
}
void Union(int p, int q){
Integer pid = root(p);
Integer qid = root(q);
if(pid == null || qid == null) return;
if(pid.equals(qid) ) return;
if(sz.get(pid) > sz.get(qid) ){
map.put(qid, pid);
sz.put(pid, sz.get(pid)+sz.get(qid));
}else {
map.put(pid, qid);
sz.put(qid, sz.get(pid)+sz.get(qid));
}
count--;
}
}
public int countComponents(int n, int[][] edges) {
UnionFind uf = new UnionFind(n);
for(int i = 0; i<edges.length; i++){
uf.Union(edges[i][0], edges[i][1]);
}
return uf.count;
}
}
323. Number of Connected Components in an Undirected Graph (leetcode)的更多相关文章
- 323. Number of Connected Components in an Undirected Graph按照线段添加的并查集
[抄题]: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of n ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- [LeetCode] 323. Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...
- Number of Connected Components in an Undirected Graph -- LeetCode
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- 323. Number of Connected Components in an Undirected Graph
算连接的..那就是union find了 public class Solution { public int countComponents(int n, int[][] edges) { if(e ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- [Locked] Number of Connected Components in an Undirected Graph
Number of Connected Components in an Undirected Graph Given n nodes labeled from 0 to n - 1 and a li ...
随机推荐
- 华东交通大学2015年ACM“双基”程序设计竞赛1003
Problem C Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- Helvetic Coding Contest 2016 online mirror A1
Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...
- day_06 再谈编码
1. 小数据池(常量池) 1.id() 通过id()查询一个变量在内存中的地址 2.is和== 1.is 判断左右两端内存地址是否一致,如果返回值是TRUE,则可以判断这两个变量值是同一对象 2.== ...
- day34 协程
1. 前提 之前我们学习了线程.进程的概念,了解了在操作系统中进程是资源分配的最小单位,线程是CPU调度的最小单位.按道理来说我们已经算是把cpu的利用率提高很多了.但是我们知道无论是创建多进程还 ...
- sshd服务及系统文件传输
一.sshd 简介 sshd= secure shell 可以通过网络在主机中开机shell的服务 客户端软件 sshd 连接方式: ssh username@ip ##文本模式的链 ...
- java——简易版build模式
参考教程:https://blog.csdn.net/fanxudonggreat/article/details/78927773 public class Computer { private S ...
- java——HashMap、Hashtable
Map:类似Python的字典 HashMap: 不支持线程的同步,即同一时刻不能有多个线程同时写HashMap: 最多只允许一条记录的键值为null,不允许多条记录的值为null HashMap遍历 ...
- grunt 合并压缩js和css文件(二)
具体node及文件配置请看: grunt 安装使用(一) 要压缩的文件 --src/ ajax.js assets.js touch.js zepto.js 目录结构: dist/ node_modu ...
- java生成临时令牌和访问令牌
public String getTicket(String logo, String productId) { String aTicket = ""; SimpleDateFo ...
- 使用cucumber & selenium实现一个简单的bddtest
1.Cucumber介绍 + feature : read requirement +scenario : testing situation,including + Given/ + when/ + ...