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. 通过css实现文本超出部分以省略号(......)代替

    一.单行溢出 1,固定宽度(非常容易) text-overflow: ellipsis; 2,不固定宽度 思路:想让这个区域成为块元素,然后不换行,溢出隐藏. display: block; whit ...

  2. C# 绘制窗体客户非客户区要用WM_PAINT和WM_NCPAINT

    窗体分为两部分:客户区(Client area)和非客户区(Non-Client area) WM_PAINT消息.OnPaint()方法.GetDC()API函数都是处理窗体客户区绘制的   而标题 ...

  3. python模块学习 hashlib

    一.hashlib概述 涉及加密服务:14. Cryptographic Services 其中 hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1.SHA224.SHA2 ...

  4. Android新建项目 默认布局改为 LinearLayout

    目前此方法仅适用于eclipse 需要修改SDK 目录 android-sdk/tools/templates/activities/BlankActivity/root/res/layout 文件: ...

  5. 【python之旅】python简介和入门

    python简介: 一.什么是python python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了打发时间,决心开发一个新的脚本解释程序, ...

  6. 在Swift中使用libxml2

    // // main.swift // C150805_libxml2r2 // http://git.oschina.net/yao_yu/Swift2015/tree/master/C150805 ...

  7. Uva_11916 Emoogle Grid

    题目链接 题意: 有个N X M的棋盘, 有K种颜色, 有B个不可涂色的位置, 共有R种涂色方案. 1)每个可涂色的位置必须涂上一种颜色 2)不可涂色位置不能涂色 3)每个位置必须从K种颜色中选出一种 ...

  8. Linux内核监控模块-3-系统调用的截获

    上一章,我们获取了系统调用表的地址,这里我们来搞点所谓“截获”的事情.所谓“截获”即是将系统调用表里的地址指向我们自己写的一个函数,系统调用先执行我们自己写的函数,处理完后,再返回原来系统调用的执行函 ...

  9. 网页错误404 or 500

    HTTP 错误 400 400 请求出错 由于语法格式有误,服务器无法理解此请求.不作修改,客户程序就无法重复此请求. HTTP 错误 401 401.1 未授权:登录失败 此错误表明传输给服务器的证 ...

  10. [BZOJ 3585] mex 【莫队+分块】

    题目链接:BZOJ - 3585 题目分析 区间mex,即区间中没有出现的最小自然数. 那么我们使用一种莫队+分块的做法,使用莫队维护当前区间的每个数字的出现次数. 然后求mex用分块,将权值分块(显 ...