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. 华东交通大学2015年ACM“双基”程序设计竞赛1007

    Problem G Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Sub ...

  2. Codeforces - 440C DFS

    搜索苦手,注意正负 #include<bits/stdc++.h> #define rep(i,j,k) for(int i = j; i <=k; i++) using names ...

  3. LeeCode(No2 - Add Two Numbers)

    LeeCode是一个有意思的编程网站,主要考察程序员的算法 第二题: You are given two non-empty linked lists representing two non-neg ...

  4. .Net默认IE版本号的两种方式

    1.直接在页面的header部位meta标签中加入如下代码 <meta http-equiv="X-UA-Compatible" content="IE=8&quo ...

  5. WIN2008R2 asp.net core的配置

    配置IIS Windows Server上通过“添加角色和功能”,桌面Windows上通过“启用和关闭Windows功能”来安装和配置IIS.确保勾选Web服务和“IIS 管理控制台”: Window ...

  6. hive表多种存储格式的文件大小差异,无重复数据

    -- 重点,目标表无重复数据 -- dbName.num_result 无重复记录 -- 插入数据 CREATE TABLE dbName.test_textfile( `key` string, ` ...

  7. 1.centos7 安装zookeeper

    1.安装jdk 1)查找jdk包: yum search java|grep jdk 2)安装: yum install -y java-1.8.0-openjdk.x86_64 2. 安装ZooKe ...

  8. GO 日志追加记录

    以追加的方式将程序输出到不同的日志文件,当日志文件超过10M大小时,自动清空文件. package tools import ( "fmt" "log" &qu ...

  9. phpstrom的xdebug开启和yii2下的分页的链接

    phpstrom的xdebug开启 1.修改php.ini文件(修改完重启apaceh) xdebug.remote_enable = onxdebug.idekey= PHPSTROM [注意:远程 ...

  10. JVM调优总结-Xmx -Xms -Xmn -Xss

    堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为操作 ...