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的更多相关文章

  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 冗余连接(C++/Java)

    题目: In this problem, a tree is an undirected graph that is connected and has no cycles. The given in ...

  4. [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 ...

  5. [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 ...

  6. LeetCode 685. Redundant Connection II

    原题链接在这里:https://leetcode.com/problems/redundant-connection-ii/ 题目: In this problem, a rooted tree is ...

  7. 【LeetCode】684. Redundant Connection 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...

  8. 684. Redundant Connection

    https://leetcode.com/problems/redundant-connection/description/ Use map to do Union Find. class Solu ...

  9. Leetcode之并查集专题-684. 冗余连接(Redundant Connection)

    Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...

随机推荐

  1. LVM 类型的 Storage Pool

    LVM 类型的 Storage Pool 不仅一个文件可以分配给客户机作为虚拟磁盘,宿主机上 VG 中的 LV 也可以作为虚拟磁盘分配给虚拟机使用. 不过,LV 由于没有磁盘的 MBR 引导记录,不能 ...

  2. ****如何优雅的用Axure装逼?高保真原型心得分享

    本文核心内容点:- 啥是高保真原型?(附简单说明原型)- Axure可以画出什么水准的高保真?(给示例,开启装逼模式)- 高保真原型图技巧:- 啥时候上高保真?适用场景 and 不适用场景 啥是高保真 ...

  3. win10+Linux18.04双系统安装

    给好多可爱的妹子重装了那么多次电脑,懒得码过程,因为我一般每次都要查一查...这次来个综合版吧,超简单,无脑操作. 首先说一下我的电脑Thinkpad + 500G 硬盘 (2014年买的老电脑) 首 ...

  4. ZOJ 1112 Dynamic Rankings【动态区间第K大,整体二分】

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112 题意: 求动态区间第K大. 分析: 把修改操作看成删除与增加 ...

  5. HDU 5905 Black White Tree(树型DP)

    题目链接  Black White Tree 树型DP,设$f[i][j]$为以$i$为根的子树中大小为$j$的连通块中可以包含的最小黑点数目. $g[i][j]$为以$i$为根的子树中大小为$j$的 ...

  6. ORA-01033: ORACLE initialization or shutdown in progress问题

    这是Oracle12c中笔者遇到的一个错误提示:ORA-01033: ORACLE initialization or shutdown in progress 错误的中文意思是:Oracle初始化未 ...

  7. ELK之收集Nginx、Tomcat的json格式日志

    1.安装Nginx yum -y install nginx vim /etc/nginx/nginx.conf # 修改日志格式为json格式,并创建一个nginxweb的网站目录 log_form ...

  8. Java 一个?格式的解决

    用Java 出现了这样的一个问题?好几天都没解决掉 然后最近一直找资料 截个图: 本来格式中时没有这个?号的,代码里面用GBK和utf-8都不能解决. 即使我加了 Str.trim(Str)去除 字符 ...

  9. mongDB的常用操作总结

    目录 常用查询: 查询一条数据 查询子元素集合:image.id gte: 大于等于,lte小于等于... 查询字段不存在的数据not 查询数量: 常用更新 更新第一条数据的一个字段: 更新一条数据的 ...

  10. eclispe集成web插件

    最近公司需要使用开源框架开发,所有下载了最新版本的eclispe工具,但是在官网下载的eclispe是不包含web插件的,无法创建web项目,需要自行集成web插件 eclipse官网下载地址:htt ...