【LeetCode】684. Redundant Connection

来源:互联网 发布:南京奥派软件 编辑:程序博客网 时间:2024/06/07 07:40

问题描述

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.

  • 思路

    判断无向图是否有环有几种方法:

    1.用并查集来判断
            若两个点的根节点相同(这两个点是父子关系),则形成环;
            若所有点中只有一个点的根节点是他本身,则根节点入度为0。

    2.DFS搜索图,图中的边只可能是树边或反向边,一旦发现反向边,则表明存在环。
    3.如果存在回路,则必存在一个子图,是一个环路。环路中所有顶点的度>=2。
           第一步:删除所有度<=1的顶点及相关的边,并将另外与这些边相关的其它顶点的度减一。
             第二步:将度数变为1的顶点排入队列,并从该队列中取出一个顶点重复步骤一。
             如果最后还有未删除顶点,则存在环,否则没有环。

    在这道题中,我们采用第一种方法。从题目看,每个数字代表一个点 ,根据第一个方法,我们需要找到第一条起点和终点有一个相同根节点的边,这条边即为结果。
    初始化时,所有的点将自己作为自己的根节点。然后开始遍历edges,更新遍历的点的根节点直到找到环为止。

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





    原创粉丝点击
    热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 火车上遇到占座不让的人怎么办 滁州婴儿打疫苗的绿本子怎么办 检查四维说宝宝嘴巴显示不清怎么办 携程订票付款成功一直没出票怎么办 手机微信支付密码忘了怎么办 存入卡里的钱却说余额不足怎么办 买高铁票身份证未通过审核怎么办 网上订演出票不配送了怎么办 百度糯米订的演出票不配送了怎么办 高铁误点改签后的车次没票了怎么办 高铁晚点没赶上下班车怎么办 上班期间单位没给交养老保险怎么办 一键启动车钥匙遥控失灵怎么办 已经订购的火车票误了火车怎么办 飞机去程取消分开买的返程怎么办 订完学生票发现使用次数过了怎么办 坐火车买学生票没带学生证怎么办 买了学生票学生证磁条没了怎么办 买了动车学生票没带学生证怎么办 身份证没磁了怎么办能买火车票 格力空调保修单丢了怎么办 格力空调保修期内坏了怎么办 国际联程航班第一程延误怎么办 大麦网演唱会的票售罄了怎么办 国内转机航班第一班延误了怎么办 联程机票下一程航班被取消怎么办 联程机票第一班航班取消怎么办 联程机票第二段航班被取消怎么办 联程航班第一程延误行李怎么办 联程航班未赶上第二程行李怎么办 高铁晚点赶不上下一趟动车怎么办? 动车晚点赶不上下一趟车怎么办 动车晚点导致没赶上下一趟怎么办 浙大三位一体选考分数报错了怎么办 报到证上时间到期了还没报到怎么办 不停的打嗝已经超过24小时怎么办 面对不给下属做主的领导怎么办 法院说退款受伤人去不了证明怎么办 e栈快递柜没收到短信怎么办 京东快递放门卫丢了怎么办 丰巢快递柜收不到取件码怎么办