In this problem, a tree is an undirected graph that is connected and has no cycles.

The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] with u < v, that represents an undirected edge connecting nodes u and v.

Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edge [u, v] should be in the same format, with u < v.

Example 1:

Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: The given undirected graph will be like this:
1
/ \
2 - 3

Example 2:

Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]
Output: [1,4]
Explanation: The given undirected graph will be like this:
5 - 1 - 2
| |
4 - 3

Note:

  • The size of the input 2D-array will be between 3 and 1000.
  • Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.

分析:UNION-FIND

 class Solution {
public int[] findRedundantConnection(int[][] edges) {
int[] parent = new int[edges.length + ];
for (int i = ; i < parent.length; i++) parent[i] = i; for (int[] edge: edges){
int f = edge[], t = edge[];
if (find(parent, f) == find(parent, t)) return edge;
else parent[find(parent, f)] = find(parent, t);
} return new int[];
} private int find(int[] parent, int f) {
if (f != parent[f]) {
parent[f] = find(parent, parent[f]);
}
return parent[f];
}
}

Redundant Connection的更多相关文章

  1. [LeetCode] Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  2. [LeetCode] Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  3. [Swift]LeetCode684. 冗余连接 | Redundant Connection

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  4. LN : leetcode 684 Redundant Connection

    lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...

  5. [LeetCode] 685. Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  6. [LeetCode] 684. Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  7. LeetCode 685. Redundant Connection II

    原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...

  8. [LeetCode] 685. Redundant Connection II 冗余的连接之 II

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  9. Leetcode之并查集专题-684. 冗余连接(Redundant Connection)

    Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...

  10. leetcode 684. Redundant Connection

    We are given a "tree" in the form of a 2D-array, with distinct values for each node. In th ...

随机推荐

  1. Fastadmin 写关联命名时,最好前后台用同一个model,方便管理(会出现命名空间问题)

    1.php think crud -t test --relation=category(外键表1) --relation=admin(外键表2) --relationforeignkey=categ ...

  2. git合并同事代码

    git 操作: 如果龙哥提交了代码,我想拉去过来,我需要的操作: 1.git fetch 2. git add . 3.git commit -m '' 提交本地的文件 4.git merge ori ...

  3. 一、基础篇--1.2Java集合-HashMap和HashSet的区别

     HashMap和HashSet的区别 1.HashMap实现的是Map接口,HashSet实现的是Set接口 2.结构不一样,一个存储的是键值对,一个存储的是对象 3.HashMap存储的值可能相同 ...

  4. [Java]算术表达式组建二叉树,再由二叉树得到算式的后序和中序表达式

    Entry类: package com.hy; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...

  5. 对opencv读取的图片进行像素调整(1080, 1920) 1.cv2.VideoCapture(构造图片读取) 2.cv2.nameWindow(构建视频显示的窗口) 3.cv2.setWindowProperty(设置图片窗口的像素) 4.video_capture(对图片像素进行设置)

    1. cv2.VideoCapture(0) #构建视频抓捕器 参数说明:0表示需要启动的摄像头,这里也可以写视频的路径 2. cv2.nameWindow(name, cv2.WINDOW_NORM ...

  6. Why convolutions always use odd-numbers as filter_size

    原文地址:https://datascience.stackexchange.com/questions/23183/why-convolutions-always-use-odd-numbers-a ...

  7. Oracle 必要的后台进程

    Oracle 必要的后台进程 Table of Contents 1. 简述 2. 必要进程 2.1. 默认启动后台进程 2.2. 哪些进程不能杀 1 简述 oralce 每次大的版本变更,后台进程都 ...

  8. dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib

    dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib Table of Contents 1. 启动时报错 ...

  9. 数据库报错:OracleDialect does not support identity key generation

    当我把数据库改为oracle时,项目报错:org.hibernate.MappingException: org.hibernate.dialect.OracleDialect does not su ...

  10. 工程变更(ENGINEERING CHANGE)

    工程变更(ENGINEERING CHANGE)是企业活动重要的管制项目之一,依照实施的时间.目的不同,其管制细分如下:  ECN (ENGINEERING CHANGE NOTICE)工程变更通知: ...