<Graph> Topological + Undirected Graph 310 Union Find 261 + 323 + (hard)305
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的更多相关文章
- PLSQL_基础系列03_合并操作UNION / UNION ALL / MINUS / INTERSET(案例)
2014-11-30 Created By BaoXinjian
- UOJ 310 黎明前的巧克力(FWT)
[题目链接] http://uoj.ac/problem/310 [题目大意] 给出一个数集,A从中选择一些数,B从中选择一些数,不能同时不选 要求两者选择的数异或和为0,问方案数 [题解] 题目等价 ...
- SQL使用union合并查询结果(转载)
1.UNION的作用 UNION 指令的目的是将两个 SQL 语句的结果合并起来.从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION 的一 ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- 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), ...
- 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 ...
- 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), ...
- 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), ...
随机推荐
- English:2019100401_Suffix"ery"
Ax_introduction source a Frech mean to "state,quality,act,place where etc" 1)After the ver ...
- react学习之js-xlsx导入和导出excel表格
前记:最近真的挺忙的,一件事接着一件,都忘了我的React项目,尽管这是一个没写概率没写离散的夜晚,我决定还是先做做我的React 好了,进入正题 项目需求,需要导入和导出表单,发现前端已经强大到无所 ...
- pycharm中将文件目录标记为sources root和sys.path.append()效果一样
之前遇到一个问题,先放上项目目录图 右边是main.py,它要引用的一个模块是在LPRNET目录下的一个文件,但是从右边可以看到pycharm有红色的线提示有错误.但是由于我们append函数将该目录 ...
- 编译出错:must be index or base register
指令 mov ds:[dx],dx 原因:上述指令使用寄存器相对寻址方式,只能使用BX,BP,SI,DI 方括号里必须是变址(index,指SI, DI)或基址(base,指BX, BP)寄存器 正确 ...
- IDEA中写MyBatis的xml配置文件编译报错的坑
IDEA中写MyBatis的xml配置文件编译报错的坑 说明:用IDEA编译工具在项目中使用Mybatis框架,编写mybatis-config.xml和Mapper.xml配置文件时,编译项目出现错 ...
- acwing 853. 有边数限制的最短路 模板
地址 https://www.acwing.com/problem/content/description/855/ 给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数. 请你求出 ...
- FFT/NTT中档题总结
被DeepinC%怕了,把一些题放到这里来 T1Normal 其实这道题放到中档题也不太合适,个人感觉真的很难,机房里好像都是颓的题解 因为期望的可加性,把每个点的贡献单独处理,即求期望深度 考虑$y ...
- IT兄弟连 HTML5教程 HTML5表单 HTML表单设计1
表单是PHP程序中最常使用的收集站点访问者信息的数据输入界面.通过表单浏览器获取用户的输入数据,并传送给Web服务器的脚本程序中,以各种不同的方式进行处理.在表单中提供了多种输入方式,包括文本输入域. ...
- koa2 从入门到进阶之路 (六)
之前的文章我们介绍了一下 koa post提交数据及 koa-bodyparser中间件,本篇文章我们来看一下 koa-static静态资源中间件. 我们在之前的目录想引入外部的 js,css,img ...
- 某小公司RESTful、共用接口、前后端分离、接口约定的实践
作者:邵磊 juejin.im/post/59eafab36fb9a045076eccc3 前言 随着互联网高速发展,公司对项目开发周期不断缩短,我们面对各种需求,使用原有对接方式,各端已经很难快速应 ...