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:

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

Example 2:

  1. Input: [[1,2], [2,3], [3,4], [1,4], [1,5]]
  2. Output: [1,4]
  3. Explanation: The given undirected graph will be like this:
  4. 5 - 1 - 2
  5. | |
  6. 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.
  1. int findParent(vector<int>& parent, int k) {
  2. if (parent[k] != k)
  3. parent[k] = findParent(parent, parent[k]);
  4. return parent[k];
  5. }
  6.  
  7. vector<int> findRedundantConnection(vector<vector<int> >& edges) {
  8. vector<int> parent;
  9. for (int i = ; i < ; i++) // 初始化
  10. parent.push_back(i);
  11.  
  12. int point1, point2;
  13. for (int j = ; j < edges.size(); j++) {
  14. point1 = findParent(parent, edges[j][]);
  15. point2 = findParent(parent, edges[j][]);
  16. if (point1 == point2)
  17. return edges[j];
  18. parent[point2] = point1;
  19. }
  20. return vector<int>(, );
  21. }

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. MapReduceV1作业生命周期图解以及与YARN基本对比

    仿照<hadoop技术内幕:深入解析MapReduce架构设计与实现原理>中的原图,我用手绘制了一份类似的图-_- 4大部分:HDFS,Client,JobTracker,TaskTrac ...

  2. 04 存储库之mongodb

    MongoDB   一 简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库1.易用性 MongoDB是一个面向文档(document-oriented)的数据库,而不是关系型数据库.不采用 ...

  3. pyqt5和qtdesign的使用

    http://blog.csdn.net/Angelasan/article/details/44917283 发现我的使用时候有点跟他不同. 我是 g: utf- -*- # Form implem ...

  4. JavaScript中的 prototype 和 constructor

    prototype属性  任何js函数都可以用作构造函数, 而构造函数需要用到prototype属性, 因此, 每个js函数F(除了ES5的Function.bind()方法返回的函数外) 都自动拥有 ...

  5. sqlserver中怎么查询字段为空的记录

    sqlserver中怎么查询字段为空的记录的两种方法: 详细介绍请查看全文:https://cnblogs.com/qianzf/ 原文博客的链接地址:https://cnblogs.com/qzf/

  6. 2018.10.02 bzoj4009: [HNOI2015]接水果(整体二分)

    传送门 整体二分好题. 考虑水果被盘子接住的条件. 不妨设水果表示的路径为(x1,y1)(x_1,y_1)(x1​,y1​),盘子表示的为(x2,y2)(x_2,y_2)(x2​,y2​) 不妨设df ...

  7. vs2010 EF4.0 访问mysql

    需要安装mysql-connector-net-6.3.5 6.8.9的安装完后在dbfirst里找不到对应的提供程序 链接字符串里需要 指定下编码(如果不是gbk的话) <add name=& ...

  8. $clog2(转)

    (转http://www.xilinx.com/support/answers/44586.html) 13.2 Verilog $clog2 function implemented imprope ...

  9. 20155218 2016-2017-2 《Java程序设计》第10周学习总结

    20155218 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 一个IP地址可以对应多个域名,一个域名只能对应一个IP地址. 在网络通讯中,第一次主动发起 ...

  10. The serializable class XXX does not declare a static final serialVersionUID field of type long

    问题: 在Eclipse中,继承类时,总是提示下面的警告(Warning),按理说警告是没有关系的,但是程序看起来老不爽了,这是强迫症的关系(呵呵) The serializable class XX ...