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. Python3编程技巧

    高效处理数据类型方法: In []: from random import randint In []: data=[randint(-,) )] In []: data Out[]: [-, -, ...

  2. 2018.09.08 AtCoder Beginner Contest 109简要题解

    比赛传送门 水题大赛? 全是水题啊!!! T1 ABC333 就是判断是不是两个数都是奇数就行了. 代码: #include<bits/stdc++.h> using namespace ...

  3. 2018.08.21 NOIP模拟 unlock(模拟+找规律)

    unlock 描述 经济危机席卷全球,L国也收到冲击,大量人员失业. 然而,作为L国的风云人物,X找到了自己的新工作.从下周开始,X将成为一个酒店的助理锁匠,当然,他得先向部门领导展示他的开锁能力. ...

  4. Django入门与实践-第16章:用户登录(完结)

    # myproject/settings.py LOGIN_REDIRECT_URL = 'home' EMAIL_BACKEND = 'django.core.mail.backends.conso ...

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

    20155218 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. dump()方 ...

  6. @WebService @WebMethod 详解

    形象图解 首先AB均需要@WebService如果 @WebService(endpointInterface="package.B") public class A implem ...

  7. struts2从浅之深(一)简介

    一.Struts2简介 1.Struts2概述                    Struts2是Apache发行的MVC开源框架.注意:它只是表现层(MVC)框架. M:model-----数据 ...

  8. 几个CSS-content的小例子

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. ZOJ2478 Encoding 2017-04-18 23:02 43人阅读 评论(0) 收藏

    Encoding Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a string containing only 'A' - 'Z', ...

  10. 菜鸟——使用bootstrap

    方法一: 直接在页面中加入bootstrap的网址,不需要做其他任何改动 <%-- Created by IntelliJ IDEA. User: JC Date: 2017/2/24 Time ...