310. Minimum Height Trees

queue:  degree为1的顶点

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

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

class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
List<Integer> result = new ArrayList<>();
if(n == 1){
result.add(0);
return result;
}
int[] degree = new int[n];
Map<Integer, List<Integer>> map = new HashMap<>();
for(int i = 0; i < n; i++) map.put(i, new ArrayList<>());
for(int[] pair : edges){
map.get(pair[0]).add(pair[1]);
map.get(pair[1]).add(pair[0]);
degree[pair[0]]++;
degree[pair[1]]++;
} Queue<Integer> queue = new LinkedList<>();
for(int i = 0; i < n; i++){
if(degree[i] == 1) queue.add(i);
} while(!queue.isEmpty()){
List<Integer> list = new ArrayList<>();
int size = queue.size();
for(int i = 0; i < size; i++){
int cur = queue.poll();
list.add(cur);
for(int nei : map.get(cur)){
degree[nei]--;
if(degree[nei] == 1) queue.add(nei);
}
}
result = list;
}
return result;
}
}

261. Graph Valid Tree

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

class Solution {
public boolean validTree(int n, int[][] edges) {
int[] nums = new int[n];
Arrays.fill(nums, -1); for(int i = 0; i < edges.length; i++){
int x = find(nums, edges[i][0]);
int y = find(nums, edges[i][1]); if(x == y) return false;
nums[x] = y;
}
return edges.length == n - 1;
} private int find(int[] nums, int i){
if(nums[i] == -1) return i;
return find(nums, nums[i]);
}
}

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不需要减一。

class Solution {
public int countComponents(int n, int[][] edges) {
int res = n;
int[] root = new int[n];
for(int i = 0; i < n; i++){
root[i] = i;
}
for(int[] edge : edges){
int x = find(root, edge[0]);
int y = find(root, edge[1]);
if(x != y){
res--;
root[y] = x;
}
}
return res;
} private int find(int[] root, int i){
while(root[i] != i) i = root[i];
return i;
}
}

305. Number of Islands II

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

class Solution {
int[][] dirs ={{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; public List<Integer> numIslands2(int m, int n, int[][] positions) {
List<Integer> res = new ArrayList<>();
if(m <= 0 || n <= 0) return res; int count = 0;
int[] roots = new int[m * n];
Arrays.fill(roots, -1); for(int[] p : positions){
int root = n * p[0] + p[1];
if(roots[root] != -1){
res.add(count);
continue;
}
roots[root] = root;
count++; for(int[] dir : dirs){
int x = p[0] + dir[0];
int y = p[1] + dir[1];
int nb = n * x + y;
if(x < 0 || x >= m || y < 0 || y >= n || roots[nb] == -1) continue; int rootSurr = find(roots, nb);
if(root != rootSurr){
roots[root] = rootSurr;
root = rootSurr;
count--;
}
}
res.add(count);
}
return res;
} public int find(int[] roots, int i){
while(i != roots[i]) i = roots[i];
return i;
}
}

<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. python的学习大纲

    python基础部分 函数 初识函数 函数进阶 装饰器函数 迭代器和生成器 内置函数和匿名函数 递归函数 常用模块 常用模块 模块和包 面向对象 初识面向对象 面向对象进阶 网络编程 网络编程 并发编 ...

  2. Vue+ElementUI的后台管理框架

    新开发的一个后台管理系统.在框架上,领导要用AdminLTE这套模板.这个其实很简单,把该引入的样式和js文件引入就可以了.这里就不多赘述了.有兴趣的可以参考:https://www.jianshu. ...

  3. ubuntu 安装在硬盘与配置

    安装 下载Ubuntu ISO文件,使用rufus制作启动U盘,重启选择这个U盘启动. 用rufus做启动盘时,提示缺少文件,点下载,找到log,进入找到下载地址,手动下载,并放到软件所在路径下的文件 ...

  4. JavaScript设计模式基础(一)

    模式的起源 模式 起源于建筑学.20世纪70年代,哈佛大学建筑学博士Christopher Alexander和他的团队花大约20年,来研究为解决同一个问题而设计出的不同建筑结构,从中发现那些高质量设 ...

  5. 2018年Code Review状态报告

    Code Review 代码评审是指在软件开发过程中,对源代码的系统性检查.通常的目的是查找系统缺陷,保证软件总体质量和提高开发者自身水平. Code Review是轻量级代码评审,相对于正式代码评审 ...

  6. linux 源设置

    ubuntu 18.04.3 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak cat > /etc/apt/sources.lis ...

  7. c++ 的namespace及注意事项

    前文 下文中的出现的"当前域"为"当前作用域"的简写 namepsace在c++中是用来避免不同模块下相同名字冲突的一种关键字,本文粗略的介绍了一下namesp ...

  8. Educational Codeforces Round 77 (Rated for Div. 2)

    A: 尽可能平均然后剩下的平摊 #include <bits/stdc++.h> using namespace std; typedef long long ll; const int ...

  9. css流星 效果

    style: .loding {     width: 100%;     height: 100%;      }   .bg{     width: 100%;     height: 100%; ...

  10. Javascript模块化开发3——Grunt之预处理

    一.grunt预处理简述 grunt的注册任务函数本身会对传入的参数和配置对象里的相关属性进行一定的预处理,方便任务函数进行操作. grunt的registerTask方法和registerMulti ...