310. Minimum Height Trees

queue:  degree为1的顶点

degree[ i ] : 和 i 顶点关联的边数。

先添加整个图,然后BFS删除每一层degree为1的节点。

  1. class Solution {
  2. public List<Integer> findMinHeightTrees(int n, int[][] edges) {
  3. List<Integer> result = new ArrayList<>();
  4. if(n == 1){
  5. result.add(0);
  6. return result;
  7. }
  8. int[] degree = new int[n];
  9. Map<Integer, List<Integer>> map = new HashMap<>();
  10. for(int i = 0; i < n; i++) map.put(i, new ArrayList<>());
  11. for(int[] pair : edges){
  12. map.get(pair[0]).add(pair[1]);
  13. map.get(pair[1]).add(pair[0]);
  14. degree[pair[0]]++;
  15. degree[pair[1]]++;
  16. }
  17.  
  18. Queue<Integer> queue = new LinkedList<>();
  19. for(int i = 0; i < n; i++){
  20. if(degree[i] == 1) queue.add(i);
  21. }
  22.  
  23. while(!queue.isEmpty()){
  24. List<Integer> list = new ArrayList<>();
  25. int size = queue.size();
  26. for(int i = 0; i < size; i++){
  27. int cur = queue.poll();
  28. list.add(cur);
  29. for(int nei : map.get(cur)){
  30. degree[nei]--;
  31. if(degree[nei] == 1) queue.add(nei);
  32. }
  33. }
  34. result = list;
  35. }
  36. return result;
  37. }
  38. }

261. Graph Valid Tree

Union Find: 并查集,这种方法对于解决连通图的问题很有效,思想是我们遍历节点,如果两个节点相连,我们将其roots值连上,这样可以帮助我们找到环,我们初始化roots数组为-1,然后对于一个pair的两个节点分别调用find函数,得到的值如果相同的话,则说明环存在,返回false,不同的话,我们将其roots值union上

  1. class Solution {
  2. public boolean validTree(int n, int[][] edges) {
  3. int[] nums = new int[n];
  4. Arrays.fill(nums, -1);
  5.  
  6. for(int i = 0; i < edges.length; i++){
  7. int x = find(nums, edges[i][0]);
  8. int y = find(nums, edges[i][1]);
  9.  
  10. if(x == y) return false;
  11. nums[x] = y;
  12. }
  13. return edges.length == n - 1;
  14. }
  15.  
  16. private int find(int[] nums, int i){
  17. if(nums[i] == -1) return i;
  18. return find(nums, nums[i]);
  19. }
  20. }

323. Number of Connected Components in an Undirected Graph

寻找无向图里的连通量。用并查集的方法。

建立一个root数组,下标和节点值相同,此时root[i]表示节点i属于group i,我们初始化了n个部分 (res = n),假设开始的时候每个节点都属于一个单独的区间,然后我们开始遍历所有的edge,对于一条边的两个点,他们起始时在root中的值不相同,这时候我们我们将结果减1,表示少了一个区间,然后更新其中一个节点的root值,使两个节点的root值相同,那么这样我们就能把连通区间的所有节点的root值都标记成相同的值,不同连通区间的root值不相同,这样也能找出连通区间的个数。

 1) x != y  :两个点原来是不相通的,现在连接了,把后一个点的root更新与x相同。

 2) x == y : 两个点是相通的,res不需要减一。

  1. class Solution {
  2. public int countComponents(int n, int[][] edges) {
  3. int res = n;
  4. int[] root = new int[n];
  5. for(int i = 0; i < n; i++){
  6. root[i] = i;
  7. }
  8. for(int[] edge : edges){
  9. int x = find(root, edge[0]);
  10. int y = find(root, edge[1]);
  11. if(x != y){
  12. res--;
  13. root[y] = x;
  14. }
  15. }
  16. return res;
  17. }
  18.  
  19. private int find(int[] root, int i){
  20. while(root[i] != i) i = root[i];
  21. return i;
  22. }
  23. }

305. Number of Islands II

用int nb = n * x + y转为一维数组

  1. class Solution {
  2. int[][] dirs ={{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
  3.  
  4. public List<Integer> numIslands2(int m, int n, int[][] positions) {
  5. List<Integer> res = new ArrayList<>();
  6. if(m <= 0 || n <= 0) return res;
  7.  
  8. int count = 0;
  9. int[] roots = new int[m * n];
  10. Arrays.fill(roots, -1);
  11.  
  12. for(int[] p : positions){
  13. int root = n * p[0] + p[1];
  14. if(roots[root] != -1){
  15. res.add(count);
  16. continue;
  17. }
  18. roots[root] = root;
  19. count++;
  20.  
  21. for(int[] dir : dirs){
  22. int x = p[0] + dir[0];
  23. int y = p[1] + dir[1];
  24. int nb = n * x + y;
  25. if(x < 0 || x >= m || y < 0 || y >= n || roots[nb] == -1) continue;
  26.  
  27. int rootSurr = find(roots, nb);
  28. if(root != rootSurr){
  29. roots[root] = rootSurr;
  30. root = rootSurr;
  31. count--;
  32. }
  33. }
  34. res.add(count);
  35. }
  36. return res;
  37. }
  38.  
  39. public int find(int[] roots, int i){
  40. while(i != roots[i]) i = roots[i];
  41. return i;
  42. }
  43. }

<Graph> Topological + Undirected Graph 310 Union Find 261 + 323 + (hard)305的更多相关文章

  1. PLSQL_基础系列03_合并操作UNION / UNION ALL / MINUS / INTERSET(案例)

    2014-11-30 Created By BaoXinjian

  2. UOJ 310 黎明前的巧克力(FWT)

    [题目链接] http://uoj.ac/problem/310 [题目大意] 给出一个数集,A从中选择一些数,B从中选择一些数,不能同时不选 要求两者选择的数异或和为0,问方案数 [题解] 题目等价 ...

  3. SQL使用union合并查询结果(转载)

    1.UNION的作用  UNION 指令的目的是将两个 SQL 语句的结果合并起来.从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION 的一 ...

  4. LeetCode Number of Connected Components in an Undirected Graph

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

  5. Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph

    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按照线段添加的并查集

    [抄题]: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of n ...

  7. LeetCode 323. Number of Connected Components in an Undirected Graph

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

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

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

随机推荐

  1. Ligg.WinOa-000: Windows运维自动化编程实战--前言

        本开源项目Ligg.WinOa是一个基于Ligg.EasyWinApp的Windows运维自动化应用.通过Ligg.EasyWinForm生成2个功能界面:管理员工具箱和用户工具箱:通过Lig ...

  2. java 线程同步方法执行与唤醒实例

    账号提钱.存钱实例方法 public class Account { private int balance; private int maxBalance; public Account(int b ...

  3. 一文解读RESTful (转)

    01 前言 回归正题,看过很多RESTful相关的文章总结,参齐不齐,结合工作中的使用,非常有必要归纳一下关于RESTful架构方式了,RESTful只是一种架构方式的约束,给出一种约定的标准,完全严 ...

  4. 如何使用终端默认情况下阻止Mac应用保存到iCloud

    当您保存要在Mac上的Pages,Numbers,TextEdit或其他基于云的应用程序中处理的文档时,该保存的默认位置是iCloud.尽管这对某些人或某些文档来说可能是一件好事,但您可能会厌倦每次更 ...

  5. 反转字符串中的单词 III

    给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest"输出: &qu ...

  6. 微信小程序的坑(持续更新中)

    参与微信小程序开发有一段时间了,先后完成信息查询类和交易类的两个不同性质的小程序产品的开发:期间遇到各种各样的小程序开发的坑,有的是小程序基础功能不断改进完善而需要业务持续的适配,有的是小程序使用上的 ...

  7. IT兄弟连 HTML5教程 CSS3揭秘 CSS常见的样式属性和值2

    3  背景属性 大多数HTML元素都允许控制背景,包括背景颜色.背景图像.背景重复.背景附件.背景位置等属性.常见的控制背景属性.值及描述如表2所示. 表2  CSS中常见的控制背景的属性 除了使用表 ...

  8. Git - Git版本库相关操作

    创建Git版本库 如下命令实现在“E:\GitCode\01_TestGit”路径下,01_TestGit项目的Git版本库. $ cd E: #将当前目录转到E盘下 $ cd GitCode    ...

  9. C#如何判断委托是实例方法还是静态方法

    一. 委托的分类 通过用什么类型的方法来声明为委托,可以分为两类: 1. 委托静态方法:把一个静态方法给委托 2. 委托实例方法:把一个实例对象的成员方法给委托 (这两个名字是博主取的,可能不是很专业 ...

  10. 利用Python开发智能阅卷系统

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 机器学习与统计学 PS:如有需要Python学习资料的小伙伴可以加 ...