图是否是树 · 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), ...
随机推荐
- linux内存查看工具
这里帮你总结了一下Linux下查看内存使用情况的多种方法~ 在做 Linux 系统优化的时候,物理内存是其中最重要的一方面.自然的,Linux 也提供了非常多的方法来监控宝贵的内存资源的使用情况.下面 ...
- Django 之 分页
1. urs.py # coding:utf-8 from django.conf.urls import url import views urlpatterns = [ # 分页练习 url(r' ...
- 怎么定位bug
测试发现bug,怎么定位?不同领域不同的测试对象,具体定位方法都不一样.自己定位bug的方法通常是以下过程: 1.发现bug,首先要查看bug的详细信息,根据描述初步分析是哪个模块哪段代码的问题 2. ...
- 20155234 2016-2017-2 《Java程序设计》第8周学习总结
20155234 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 java.util.loggging包提供了日志功能相关类与接口. 使用日志的起点是Logg ...
- Linux内核静态映射表的建立过程
/* * 平台: s5pv210 * 内核版本号: 2.6.35.7 */ kernel/arch/arm/mach-s5pv210/mach-smdkc110.c 这个文件是由三星在 ...
- 10055 - Hashmat the Brave Warrior & 各数据类型所占字节数 (C语言)
Problem A Hashmat the brave warrior Input: standard input Output: standard output Hashmat is a brave ...
- graphql-binding openapi 集成demo
类似的将openapi 转换为graphql api 的也有 https://github.com/yarax/swagger-to-graphql 基本项目 参考代码 https://github. ...
- VS2017新建windows控制台程序打印中文乱码问题
最近刚换上VS2017,由于手头又要做个MFC的程序,所以写控制台程序做功能测试,然后发现居然乱码了. 于是用VS2017新建windows控制台应用程序,在main函数种加一句printf(&quo ...
- bzo j4825 [Hnoi2017]单旋
Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据 结构,因为代码好写,功能多,效率高,掌握这种数据结构成为了 H 国的 ...
- pandas 的操作表单
最近做点东西,发现pandas处理表单那点东西都忘得差不多了,又花了半个小时过了一遍 import pandas as pdimport numpy as np data = pd.ExcelFile ...