LeetCode Graph Valid Tree
原题链接在这里:https://leetcode.com/problems/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), write a function to check whether these edges make up a valid tree.
For example:
Given n = 5
and edges = [[0, 1], [0, 2], [0, 3], [1, 4]]
, return true
.
Given n = 5
and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]]
, return false
.
题解:
Union-Find, 与Number of Islands II相似.
Check is edges count is equal to n-1(There is only one cluster after merging).
然后判断有没有环,若是find(edge[0],edge[1])返回true 说明edge[0], edge[1]两个点之前就连在一起了.
Time Complexity: O(n*logn). Space: O(n).
AC Java:
- public class Solution {
- public boolean validTree(int n, int[][] edges) {
- if(edges == null || edges.length != n-1){
- return false;
- }
- UnionFind tree = new UnionFind(n);
- for(int [] edge : edges){
- if(!tree.find(edge[0], edge[1])){
- tree.union(edge[0], edge[1]);
- }else{
- return false;
- }
- }
- return true;
- }
- }
- class UnionFind{
- int count, n;
- int [] size;
- int [] parent;
- public UnionFind(int n){
- this.n = n;
- this.count = n;
- size = new int[n];
- parent = new int[n];
- for(int i = 0; i<n; i++){
- parent[i] = i;
- size[i] = 1;
- }
- }
- public boolean find(int i, int j){
- return root(i) == root(j);
- }
- private int root(int i){
- while(i != parent[i]){
- parent[i] = parent[parent[i]];
- i = parent[i];
- }
- return i;
- }
- public void union(int p, int q){
- int i = root(p);
- int j = root(q);
- if(size[i] > size[j]){
- parent[j] = i;
- size[i] += size[j];
- }else{
- parent[i] = j;
- size[j] += size[i];
- }
- this.count--;
- }
- public int size(){
- return this.count;
- }
- }
LeetCode Graph Valid Tree的更多相关文章
- [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: 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), ...
- [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] 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#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 ...
- [LeetCode] 261. Graph Valid Tree _ Medium tag: BFS
Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), w ...
- 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), ...
- [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), ...
- 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 ...
随机推荐
- POJ 3276 (开关问题)
题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...
- ACM Arithmetic Expression
Description Given N arithmetic expressions, can you tell whose result is closest to 9? Input Line 1: ...
- 【POJ】3744 Scout YYF I
http://poj.org/problem?id=3744 题意:直线上n个地雷,n<=10,范围在[1, 100000000],每一次有p的概率向前走一步,1-p的概率向前走两步,问安全通过 ...
- Codeforces Round #200 (Div. 2) E. Read Time(二分)
题目链接 这题,关键不是二分,而是如果在t的时间内,将n个头,刷完这m个磁盘. 看了一下题解,完全不知怎么弄.用一个指针从pre,枚举m,讨论一下.只需考虑,每一个磁盘是从右边的头,刷过来的(左边来的 ...
- mysql in 命令
SQL: select * from table where id IN (3,6,9,1,2,5,8,7); SQL: select * from table where id IN ($str); ...
- 谷歌(GDG):智能技术在物联网及移动互联网中的最新应用讲座
谷歌开发者社区GDG(原谷歌技术用户组GTUG),将于11月23日(周六)下午 1:30-5:00,在北京翠宫饭举办一场智能技术在物联网及移动互联网中的最新应用讲座,培训讲座中将通过三个专题与众 ...
- 使用diff制作补丁
1.制作补丁包 命令格式 diff -uNr oldfile.c newfile.c > x.patch 2.打补丁 命令格式 patch -p0 < x.patch 总结一下:单个文件 ...
- 一个不错的安卓下ssh客户端
1.使用安卓作为ssh客户端连接ssh服务器 软件名:JuiceSSH 版本 :1.4.8 大小 :4.22 M 百度网盘地址:JuiceSSH_1.4.8.apk 或 JuiceSSH_1 ...
- #define is unsafe——I
I. #define is unsafe Have you used #define in C/C++ code like the code below? #include <stdio.h&g ...
- tab1
<html> <head> <meta charset="UTF-8"> <title>tab</title> < ...