Leetcode算法学习日志-684 Redundant Connection
来源:互联网 发布:web编程语言有哪些 编辑:程序博客网 时间:2024/06/06 22:54
Leetcode 684 Redundant Connection
题目原文
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]
withu < v
, that represents an undirected edge connecting nodesu
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:
题意分析
给定义一组点之间的链接关系,表示一个图,该图是由一棵树添加一条边得到的,添加边以前没有,因此一定得到一个圈。本题是无向图。
解法分析
本题采用了两种方法,
- 深度优先搜索
- Union Find(并查集)
深度优先搜索
该方法沿着树的一条路往下走,先保存第一个连接对的两个元素于set中,并将该对从集合中删除,重新遍历连接对集合,如果下一个连接对两个元素都在set中,说明走出了一个回路,返回该对即为结果;如果有一个元素在set中,则可以延这条路继续走,并将该对删除;如果两个都不在set中,则可以暂时跳过。C++代码如下:
class Solution {public: vector<int> findRedundantConnection(vector<vector<int>>& edges) { unordered_set<int> attened; vector<int> res; if(edges.size()<3) return res; attened.insert(edges[0][0]); attened.insert(edges[0][1]); for(int i=1;i<edges.size();i++){ if(attened.count(edges[i][0])&&attened.count(edges[i][1])) return edges[i]; else if((!attened.count(edges[i][0]))&&(!attened.count(edges[i][1]))) continue; else{ if(attened.count(edges[i][0])) attened.insert(edges[i][1]); else attened.insert(edges[i][0]); edges.erase(edges.begin()+i); i=0; } } }};
Union Find
并查集可以根据已经Union后形成的连接情况判断两个点是否连接,同时也可以在Union过程中判断两个点是否连接,以避免重复添加连接关系使他们连接成圈。本题最后添加的那个使得树产生圈的边就可以通过边union边检测的方式检查出来。C++代码如下:
class Solution {public: int find(int a,int *P){ int res=a; while(res!=P[res]) res=P[res]; return res; } vector<int> findRedundantConnection(vector<vector<int>>& edges) { int *parent=new int[edges.size()+1]; for(int i=1;i<=edges.size();i++){ parent[i]=i; } for(auto e:edges){ if(find(e[0],parent)==find(e[1],parent)) return e; parent[find(e[1],parent)]=find(e[0],parent); } }};
union过程中是将find得到的根节点间确定新的父子关系。- Leetcode算法学习日志-684 Redundant Connection
- LeetCode #684 Redundant Connection
- leetcode 684|685. Redundant Connection 1|2
- leetcode 684. Redundant Connection
- Redundant Connection(leetcode)
- 【LeetCode】684. Redundant Connection
- [LeetCode]684. Redundant Connection
- 【LeetCode】Redundant Connection
- leetcode练习 Redundant Connection
- leetcode Redundant Connection
- leetcode 684. Redundant Connection解法
- LeetCode 684. Redundant Connection [Medium]
- [LeetCode]685. Redundant Connection II
- LeetCode题解系列--684. Redundant Connection
- LeetCode题解系列--685. Redundant Connection II
- Redundant Connection
- 算法分析与设计丨第九周丨LeetCode(13)——Redundant Connection(Medium)
- 并查集算法解决Redundant Connection I&II
- hdu 2844 coins 多重背包
- ECharts的自适应位置
- 哈尔滨理工大学第七届程序设计竞赛决赛 A 所有情况的和
- HTTP协议浅析(下): 使用HTTP协议实现通信
- 蓝桥杯 算法训练 拦截导弹
- Leetcode算法学习日志-684 Redundant Connection
- 常见的五类排序算法图解和实现(归并类:二路归并排序)
- Oracle数据库创建序列
- 数据结构实验之查找一:二叉排序树
- CocosCreator项目学习系列<二>关于Button(添加事件)输入控制交互条件的触发_实现虚拟按钮控制_JavaScript
- 740. Delete and Earn
- 如果有人问你爬虫抓取技术的门道,请叫他来看这篇文章
- 多线程socket通信,信号量控制发送多线程发送顺序,测试通信延时
- Ciclop开源3D扫描仪软件---Horus源码分析之src\horus\engine\calibration\autocheck.py