May-26-2019

UF做算是白给的

压缩下,没Quick union(否则要几把weight),然后比较integer用equals稳点= =顺便尽量别int[]能MAP最好MAP

class Solution {

    public class UF {
Map<Integer, Integer> roots;
Map<Integer, Integer> size;
int num;
public UF(int n) {
roots = new HashMap<>();
size = new HashMap<>();
num = n;
for (int i = 0; i < n; i ++) {
roots.put(i, i);
size.put(i, 1);
}
} public Integer find(Integer n) {
if (!roots.containsKey(n)) return null;
Integer root = n;
while (!root.equals(roots.get(root))) {
roots.put(root, roots.get(roots.get(root)));
root = roots.get(root);
}
return root;
} public void union(Integer a, Integer b) {
Integer rootA = find(a);
Integer rootB = find(b); if (rootA == null || rootB == null || rootA.equals(rootB)) return; Integer sizeA = size.get(rootA);
Integer sizeB = size.get(rootB);
if (sizeA > sizeB) {
roots.put(rootB, rootA);
size.put(rootA, sizeB + sizeA);
} else {
roots.put(rootA, rootB);
size.put(rootB, sizeA + sizeB);
}
this.num --;
} }
public int countComponents(int n, int[][] edges) {
if (edges.length == 0 || edges[0].length == 0) return n; UF uf = new UF(n);
for (int[] edge : edges) {
Integer a = edge[0];
Integer b = edge[1];
uf.union(a, b);
}
return uf.num;
}
}

323. Number of Connected Components in an Undirected Graph的更多相关文章

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

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

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

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

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

  7. LeetCode Number of Connected Components in an Undirected Graph

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

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

  9. [Swift]LeetCode323. 无向图中的连通区域的个数 $ 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), ...

随机推荐

  1. wamp使用方法【总】

    Apache与php配置:我们把php-5.2.9-Win32.zip解压到C盘根目录下,把文件夹名字改成PHP,这样方便一下. 1. 找到PHP目录下的“php.ini-dist”或者“php.in ...

  2. ZOJ 1423 (Your)((Term)((Project))) (模拟+数据结构)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=423 Sample Input 3(A-B + C) - (A+(B ...

  3. 受教了,memcache比较全面点的介绍,受益匪浅,适用memcached的业务场景有哪些?memcached的cache机制是怎样的?在设计应用时,可以通过Memcached缓存那些内容?

    基本问题 1.memcached的基本设置 1)启动Memcache的服务器端 # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 ...

  4. 【读书笔记】【CLR via C#】【第一章】The CLR’s Execution Model

    内容提要 本章的目的是对.Net 框架的设计做一个总体的介绍,包括介绍框架中使用的一些技术.定义一些术语.同时会展示从源代码生成应用程序(或者一些包含了一些自定义类型的可以发布的组件),并且会解释程序 ...

  5. 2016022604 - redis命令介绍

    Redis keys命令用于在Redis的管理键. Redis keys命令使用语法如下所示: redis最新版本目前是3.0.7 redis 127.0.0.1:6379> COMMAND K ...

  6. 常用原生JS兼容性写法汇总

    1.添加事件方法 addHandler:function(element,type,handler){ if(element.addEventListener){//检测是否为DOM2级方法 elem ...

  7. 上传XML文件字符编码问题

    1.上传的XML文件的空格的字符编码和倒入到数据库的空格的字符编码不是一种编码格式,导致导入到数据库的数据和XML文件的数据不一致的情况,进而使展示到界面上的数据在进行搜索时不能搜索出来.解决办法: ...

  8. 区分html与css中的属性

    CSS中id与Class的区别 1.在CSS文件里书写时,ID加前缀"#":CLASS用"." 2.id一个页面只可以使用一次:class可以多次引用. 3.I ...

  9. [BZOJ 3894] 文理分科 【最小割】

    题目链接:BZOJ - 3894 题目分析 最小割模型,设定一个点与 S 相连表示选文,与 T 相连表示选理. 那么首先要加上所有可能获得的权值,然后减去最小割,即不能获得的权值. 那么对于每个点,从 ...

  10. [BZOJ 1029] [JSOI2007] 建筑抢修 【贪心】

    题目链接:BZOJ - 1029 题目分析 使用一种贪心策略. 现将任务按照deadline从小到大排序. 然后枚举每一个任务,如果当前消耗的时间加上完成这个任务的时间不会超过这个任务的deadlin ...