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. Codeforces - 151C 质因子分解

    显然只需要能跑到第二个因子就赢了 需要特判非平凡因子 常数优化:不用求出所有因子,跑完第二个素数就行了 #include<bits/stdc++.h> using namespace st ...

  2. A. Free Cash

    A. Free Cash time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. Linux磁盘分区管理

    1.分区步骤          fdisk -l                                  查看系统中的磁盘 fdisk /dev/vdb                   ...

  4. LeeCode(No2 - Add Two Numbers)

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

  5. 2.Servlet(一)

    1.Servlet的编写.访问过程: (1)编写部署Servlet程序: 编写源文件->编译类文件->部署程序->运行->Servlet处理请求,返回响应. (2)Eclips ...

  6. my28_mysql内存占用过高降低的方法

    对mysql做压力测试,测试完之后,mysql的内存一直不下降 $ free -m total used free shared buff/cache available Mem: Swap: # t ...

  7. 5-----Scrapy框架中Spiders用法

    Spider类定义了如何爬去某个网站,包括爬取的动作以及如何从网页内容中提取结构化的数据,总的来说spider就是定义爬取的动作以及分析某个网页 工作流程分析 1.以初始的URL初始化Request, ...

  8. git使用标准

    git 使用规范 团队开发中,要遵循一个合理.清晰的git使用流程非常重要的.否则每个人提交一堆杂乱我长的commit,项目很快就变得难以协调和维护 第一步:创建新分支 首先,每一次开发新功能,都应该 ...

  9. Android多线程源码学习笔记一:handler、looper、message、messageQueue

    最近在学习Android多线程相关知识的源码,现在把自己的笔记整理一下,写出来加深印象. Android多线程通讯的核心是handler.looper.message.messageQueue,这篇文 ...

  10. Json的访问

    JSON:JavaScript 对象表示法(JavaScript Object Notation) 写法:名称/值对 访问方法:可以通过 data.名称 访问,也可以通过 data['名称'] 访问 ...