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 = 5 and edges = [[0, 1], [1, 2], [3, 4]]

     0          3
| |
1 --- 2 4 Output: 2

Example 2:

Input: n = 5 and edges = [[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)的更多相关文章

  1. 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 ...

  2. LeetCode 323. Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  3. [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), ...

  4. 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...

  5. 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), ...

  6. 323. Number of Connected Components in an Undirected Graph

    算连接的..那就是union find了 public class Solution { public int countComponents(int n, int[][] edges) { if(e ...

  7. [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), ...

  8. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  9. [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 ...

随机推荐

  1. restframework 的一些操作

    路飞学城项目: 1 Vue 2 restframework框架(一周) 3 学城项目(一周-两周) day98 1 CBV(class based view) 与 FBV(function based ...

  2. linux系统延时和定时任务

    系统延时任务延时任务:只做一次的at命令: 系统定时及延时任务 延时任务:**有输出任务**不会输出到终端上而是发送邮件给你/var/mail/root/执行 mail at          时间 ...

  3. 微软原版SQL Helper

    代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-- ...

  4. 浏览器存储localStorage、sessionStorage、cookie

    localStorage和sessionStorage浏览器支持IE8+(测试IE8不行): localStorage:用于长久保存整个网站的数据,保存的数据没有过期时间,直到手动去除 session ...

  5. 如何 安装Yii2的高级应用程序模板

    参考 https://blog.csdn.net/youngqj/article/details/46689051

  6. hashcode详解--转发

    序言 写这篇文章是因为在看hashMap源码时遇到有什么hashcode值,然后就去查,脑袋里面是有映像的,不就是在Object中有equals和hashcode方法嘛,这在学java基础的时候就遇到 ...

  7. Unity3d 破解

    在官网上或者其他地方下载unity3d后 运行注册机,需要详细的设置可以点击edit   一.点击browse,选择安装unity目录下的Editor文件夹 二.选择path,会提示Pathed 三. ...

  8. inventor卸载不干净

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  9. Murano Weekly Meeting 2015.08.25

    Meeting time: 2015.August.25th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting summ ...

  10. PyCharm5 破解汉化

    作者博文地址:https://www.cnblogs.com/liu-shuai/ 破解: 将下列序列号复制到软件激活界面即可破解. 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0 ...