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 ...
随机推荐
- Nginx根据目录自动生成二级域名
前言: 每次创建二级域名如果都修改一次配置文件的话,项目多了会带来很多不必要的工作量,如果能够在一个web目录下创建一个文件夹并且自动生成文件目录的话,那真是极好的,本文就基于Nginx贴出这 ...
- 解决mac下ssh空闲一段时间自动断开的问题
之前在公司就遇到过这种问题,使用ssh登录linux服务器后,在后台放置一段时间,会发现会自动断开,解决的方法很简单: vim /etc/ssh/ssh_config 添加这2句即可 ServerAl ...
- bios-----> grub
系统有两块硬盘, 第一块安装的win7, 第二块安装ubuntu 默认从sda加载grub 如果在bios页面选择从sdb启动,会找不到grub 进入原来的sda系统, grub-install / ...
- 创建本地Git并提交到码云
概述 安装Git,使用Git Bash创建本地Git全局用户名,提交远程代码时将以此用户名显示git config --global --replace-all user.email "it ...
- IIS 配置asp.net 环境
打开Internet信息服务管理器,在左侧点击“应用程序池”,下面将以asp.net v4.0 程序池为例 在中间的“ASP.NET V4.0”上右键“高级选项”,展开“进程模型”,找“标识”选项,然 ...
- g++ 出现 undefined reference to ......
g++ 出现 undefined reference to ...... 检查/usr/local/lib /usr/lib 发现已经存在相应的库文件 那么,问题可能出现在g++链接次序上,即先链接 ...
- linux_api之文件属性
本篇索引:1.引言2.文件类型3.获取文件属性的函数,stat.fstat.lstat4.超级用户(root用户)和普通用户5.进程与用户ID6.文件权限的检查7.新创建的的文件和目录的所有权8.ac ...
- 但是你没有【But you didn't.】【by Anonymous】
作者是一位普通的美国妇女,她的丈夫在女儿4岁时应征入伍去了越南战场,从此她便和女儿相依为命.后来,她的丈夫.孩子的爸爸不幸阵亡.她终身守寡,直至年老病逝.她女儿在整理遗物时发现了母亲当年写给父亲的这首 ...
- java多线程优先级问题
java 中的线程优先级的范围是1-10,默认的优先级是5.“高优先级线程”会优先于“低优先级线程”执行. 例子: package com.ming.thread.threadpriority; pu ...
- Linux下的NFS快速配置教程与安全策略
[51CTO专稿]在Linux下实现文件共享有多种方式,NFS就是其中之一.网络文件系统(NFS)协议是由Sun MicroSystem在20世纪80年代为了提供对共享文件的远程访问而设计和实现的.该 ...