leetcode 684. Redundant Connection
We are given a "tree" in the form of a 2D-array, with distinct values for each node.
In the given 2D-array, each element pair [u, v] represents that v is a child of u in the tree.
We can remove exactly one redundant pair in this "tree" to make the result a tree.
You need to find and output such a pair. If there are multiple answers for this question, output the one appearing last in the 2D-array. There is always at least one answer.
Example 1:
Input: [[1,2], [1,3], [2,3]]
Output: [2,3]
Explanation: Original tree will be like this:
1
/ \
2 - 3
Example 2:
Input: [[1,2], [1,3], [3,1]]
Output: [3,1]
Explanation: Original tree will be like this:
1
/ \\
2 3
Note:
The size of the input 2D-array will be between 1 and 1000.
Every integer represented in the 2D-array will be between 1 and 2000.
并查集,判断一下就好了 (比如线段[a,b],如果a在集合中,b也在集合中那么这条边就要删除)
class Solution {
public:
int fa[1100];
void init() {
for (int i = 0; i < 1001; ++i) fa[i] = i;
}
int findfa(int x) {
if (x == fa[x]) return x;
return fa[x] = findfa(fa[x]);
}
int Union(int a, int b) {
int x = findfa(a);
int y = findfa(b);
if (x == y) return 1;
if (x < y) fa[y] = x;
else fa[x] = y;
return 0;
}
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
init();
vector<int>v;
for (int i = 0; i < edges.size(); ++i) {
int a = edges[i][0];
int b = edges[i][1];
if (Union(a, b)) {
v.push_back(a);
v.push_back(b);
}
}
return v;
}
};
You are here!
Your runtime beats 100.00 % of cpp submissions. 哈哈
leetcode 684. Redundant Connection的更多相关文章
- LN : leetcode 684 Redundant Connection
lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...
- [LeetCode] 684. Redundant Connection 冗余的连接
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- 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 ...
- [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 ...
- [LeetCode] 685. Redundant Connection II 冗余的连接之二
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
- LeetCode 685. Redundant Connection II
原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...
- 【LeetCode】684. Redundant Connection 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...
- 684. Redundant Connection
https://leetcode.com/problems/redundant-connection/description/ Use map to do Union Find. class Solu ...
- Leetcode之并查集专题-684. 冗余连接(Redundant Connection)
Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...
随机推荐
- Mychael原创题 洛谷T23923 Mychaelの水题 【题解】
原题链接 题目大意: 有来自三个地区的人各a,b,c位,他们排成了一排.请问有多少种不同类型的排法,使得相邻的人都来自不同的地区 \(a,b,c<=200\) 答案取模 题解 弱弱的标程解法 设 ...
- 刷题总结——竞赛得分(ssoj)
题目: 题目描述 ZZH 在经历了无数次学科竞赛的失败以后,得到了一个真理:做一题就要对一题!但是要完全正确地做对一题是要花很多时间(包括调试时间),而竞赛的时间有限.所以开始做题之前最好先认真审题, ...
- 如何解决maven archetype加载太慢的方法
有时候在构建maven项目时,archetype加载不出来,一直在Retrieving archetype...,现象如下: 有两种解决方法: ①点击链接下载:http://files.cnblogs ...
- 了解Chrome扩展程序开发--摘抄
了解Chrome扩展程序开发 2018-01-11 边城到此莫若 鸡蛋君说前端 首先,我尝试来用简单几句话描述一下Chrome扩展程序: Chrome扩展主要用于对浏览器功能的增强,它强调与浏览器相结 ...
- 安卓解析JSON文件
安卓解析JSON文件 根据JOSN文件的格式,文件只有两种数据,一是对象数据,以 {}为分隔,二是数组,以[]分隔 以下介绍安卓如何解析一个JSON文件,该文件存放在assets目录下,即:asset ...
- web信息搜索之目标扫描篇
https://blog.csdn.net/dongfei2033/article/details/78175421
- android widgets控件
1.TextView 类似,C#里的lable,显示一段文本 <TextView android:id="@+id/textView2" android:layout_wid ...
- python socket非阻塞及python队列Queue
一. python非阻塞编程的settimeout与setblocking+select 原文:www.th7.cn/Program/Python/201406/214922.shtml 侧面认证Py ...
- python和linux的环境设置/PATH
一.python的环境设置 1.输出path列表: pi@raspberrypi:~$ pythonPython 3.4.0 (default, Jul 1 2014, 09:37:01) [GCC ...
- luogu P1351 联合权值
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...