图是否是树 · Graph Valid Tree
[抄题]:
给出 n 个节点,标号分别从 0 到 n - 1 并且给出一个 无向边的列表 (给出每条边的两个顶点), 写一个函数去判断这张`无向`图是否是一棵树。
给出n = 5 并且 edges = [[0, 1], [0, 2], [0, 3], [1, 4]], 返回 true.
给出n = 5 并且 edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], 返回 false.
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
[一句话思路]:
树中不能有环,两点+老大哥三角成环。遍历所有边并且缩点,一旦出现公共祖先就退出。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 树的基本性质是: 边= 点数 - 1,若不符合则退出
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
树中不能有环。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
两点+老大哥三角成环,union find可以找老大哥。
[关键模板化代码]:
class UnionFind {
HashMap<Integer, Integer> father = new HashMap<>();
UnionFind(int n) {
for (int i = 0; i < n; i++) {
father.put(i,i);
}
}
int compressed_find(int x) {
//find ultimate parent
int parent = x;
while (parent != father.get(parent)) {
parent = father.get(parent);
}
//change 2 ultimate parent
int temp = -1;
int fa = x;
while (fa != father.get(fa)) {
temp = father.get(fa);
father.put(fa,parent);
fa = temp;
}
return parent;
}
void union (int x, int y) {
int fa_x = compressed_find(x);
int fa_y = compressed_find(y);
if (fa_x != fa_y) {
father.put(fa_x,fa_y);
}
}
}
并查集class
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
public class Solution {
/*
* @param n: An integer
* @param edges: a list of undirected edges
* @return: true if it's a valid tree, or false
*/
//class
class UnionFind {
HashMap<Integer, Integer> father = new HashMap<>();
UnionFind(int n) {
for (int i = 0; i < n; i++) {
father.put(i,i);
}
}
int compressed_find(int x) {
//find ultimate parent
int parent = x;
while (parent != father.get(parent)) {
parent = father.get(parent);
}
//change 2 ultimate parent
int temp = -1;
int fa = x;
while (fa != father.get(fa)) {
temp = father.get(fa);
father.put(fa,parent);
fa = temp;
}
return parent;
}
void union (int x, int y) {
int fa_x = compressed_find(x);
int fa_y = compressed_find(y);
if (fa_x != fa_y) {
father.put(fa_x,fa_y);
}
}
}
public boolean validTree(int n, int[][] edges) {
//corner case is special
if (edges.length != n - 1) {
return false;
}
UnionFind uf = new UnionFind(n);
for (int i = 0; i < edges.length; i++) {
if (uf.compressed_find(edges[i][0]) ==
uf.compressed_find(edges[i][1])) {
return false;
}
uf.union(edges[i][0], edges[i][1]);
}
return true;
}
}
解法2:
323进化而来
添加每一条边 root1 == root0代表有环,不行
count > 1代表分块,不行
class Solution {
public boolean validTree(int n, int[][] edges) {
//use union find
//ini
int count = n;
int[] roots = new int[n];
//cc
if (n == 0 || edges == null) return true;
//initialization the roots as themselves
for (int i = 0; i < n; i++)
roots[i] = i;
//add every edge
for (int[] edge : edges) {
int root0 = find(edge[0], roots);
int root1 = find(edge[1], roots);
if (root0 == root1) return false;
//connect but is not merge
roots[root0] = root1;
count--;
}
//return
return count == 1;
}
public int find(int id, int[] roots) {
while (id != roots[id])
id = roots[roots[id]];
return id;
}
}
图是否是树 · Graph Valid Tree的更多相关文章
- [Swift]LeetCode261.图验证树 $ Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [Locked] Graph Valid Tree
Graph Valid Tree Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] 261. Graph Valid Tree 图是否是树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- 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), ...
- 261. Graph Valid Tree
题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...
- [LeetCode#261] Graph Valid Tree
Problem: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair o ...
- Graph Valid Tree -- LeetCode
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
随机推荐
- 关于C#引用dll动态链接库文件的注释问题
1.dll动态库文件项目生成属性中要勾选"XML文档文件" 注意:1).要选中项目,查看项目属性,选中解决方案是找不到的.2).XML文件的名字不要修改. 2.添加引用时XML文件 ...
- LOJ2316. 「NOIP2017」逛公园【DP】【最短路】【思维】
LINK 思路 因为我想到的根本不是网上的普遍做法 所以常数出奇的大,而且做法极其暴力 可以形容是带优化的大模拟 进入正题: 首先一个很显然的思路是如果在合法的路径网络里面存在零环是有无数组解的 然后 ...
- http的报文结构和状态码的含义
HTTP响应报文解剖 响应报文结构 HTTP的响应报文也由三部分组成(响应行+响应头+响应体): 以下是一个实际的HTTP响应报文: ①报文协议及版本: ②状态码及状态描述: ③响应报文头,也是由多个 ...
- JDBC的操作步骤
JDBC的操作步骤 一.什么是JDBC JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问 ...
- laravel 创建自定义全局函数
全局函数的实现是依靠在初始化的时候,将helps.php或者functions.php直接进行了加载.而Laravel中bootstrap/autoload.php(laravel 5.5 貌似没有这 ...
- flow 类型生成工具 flow-typed 简单使用
flow 是一个javascript 的静态检查工具,flow-typed 为我们提供了三方类似type 的生成 安装flow-typed 使用全局安装 yarn global add flow-ty ...
- MySQL 数据类型(float)的注意事项
摘要: 今天左哥问起一个float浮点数类型的问题,这个类型用的不多,所以也不太了解,现在打算测试下. 知识点: float:浮点数,单精度,占4字节. 测试 root@local ...
- 关于AM335X移植SDIO WIFI的简易教程(转)
最近应一个朋友邀请,帮他移植了SDIO WIFI到3.2版本内核.因为之前已经成功移植了3.14内核,所以整个过程花了一个下午就完成了.话不多说,先交待一下平台: CPU:TI AM3352 600M ...
- redhat 连接mysql数据库Can't get hostname for your address
redhat 连接mysql数据库Can't get hostname for your address Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQ ...
- vim自定义配置之常规设置
vimConfig/plugin/general-operation.vim "快速关闭 map <S-Q> :q<CR>:q<CR>:q<CR&g ...