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.
int findParent(vector<int>& parent, int k) {
if (parent[k] != k)
parent[k] = findParent(parent, parent[k]);
return parent[k];
} vector<int> findRedundantConnection(vector<vector<int> >& edges) {
vector<int> parent;
for (int i = ; i < ; i++) // 初始化
parent.push_back(i); int point1, point2;
for (int j = ; j < edges.size(); j++) {
point1 = findParent(parent, edges[j][]);
point2 = findParent(parent, edges[j][]);
if (point1 == point2)
return edges[j];
parent[point2] = point1;
}
return vector<int>(, );
}

Graph-684. Redundant Connection的更多相关文章

  1. LN : leetcode 684 Redundant Connection

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

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

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

  3. leetcode 684. Redundant Connection

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

  4. LeetCode 684. Redundant Connection 冗余连接(C++/Java)

    题目: In this problem, a tree is an undirected graph that is connected and has no cycles. The given in ...

  5. 【LeetCode】684. Redundant Connection 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...

  6. 684. Redundant Connection

    https://leetcode.com/problems/redundant-connection/description/ Use map to do Union Find. class Solu ...

  7. [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 ...

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

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

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

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

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

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

随机推荐

  1. Ubuntu的人性化配置

    1.更改Ubuntu命令行提示符颜色,在~/.bashrc中添加如下命令行: PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[ ...

  2. 并发编程(五)LockSupport

    并发编程(五)LockSupport LockSupport 提供 park() 和 unpark() 方法实现阻塞线程和解除线程阻塞,实现的阻塞和解除阻塞是基于"许可(permit)&qu ...

  3. NSNotificationCenter 注意

    成对出现 意思很简单,NSNotificationCenter消息的接受线程是基于发送消息的线程的.也就是同步的,因此,有时候,你发送的消息可能不在主线程,而大家都知道操作UI必须在主线程,不然会出现 ...

  4. Activiti中23张表的含义

    1.与流程定义相关的4张表: 2.与执行任务相关的5张表: 3.与流程变量相关的2张表

  5. Criteria查询

    1.Criteria表达式 Criteria c=session.createCriteria(User.class); List result=c.list(); Iterator it=resul ...

  6. 功率谱密度(PDS)的MATLAB分析

    功率谱密度(PSD),它定义了信号或者时间序列的功率如何随频率分布.这里功率可能是实际物理上的功率, 或者更经常便于表示抽象的信号被定义为信号数值的平方,也就是当信号的负载为1欧姆(ohm)时的实际功 ...

  7. mac windows蓝牙问题

    如果是win7.win8或win10三者的64位版本,可以下载驱动解决:http://file2.mydrivers.com/2014/notebook/apple_broadcom_bluetoot ...

  8. POJ3258 River Hopscotch 2017-05-11 17:58 36人阅读 评论(0) 收藏

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13598   Accepted: 5791 ...

  9. STL中的内存与效率

    STL中的内存与效率 1. 使用reserve()函数提前设定容量大小,避免多次容量扩充操作导致效率低下.  关于STL容器,最令人称赞的特性之一就是是只要不超过它们的最大大小,它们就可以自动增长到足 ...

  10. log4j自动加载原理

    java虚拟机加载log4j的类(LogManager.class)后,执行静态代码块,这个类中的静态代码块,会load log4j的配置文件,依次加载log4j.xml,log4j.properti ...