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. poj-2728Desert King(最优比率生成树)

    David the Great has just become the king of a desert country. To win the respect of his people, he d ...

  2. 大逃杀(树上dp)

    这道题和宝藏差不多吧,转移的时候比较麻烦的. 代码中分量很多种情况. h更新比较麻烦 这两幅图表示了双边更新中3,4连个h更新,下面比较好理解的吧. #include<cstring> # ...

  3. Linux(7):用户管理

    用户管理 让一个脚本或命令开机自启动的方法: # 方法一: 把脚本放到 /etc/rc.local 中 # 方法二: 把脚本或命令通过 chkconfig 管理 # 如何让一个脚本被 chkconfi ...

  4. PHP的Trait机制

    PHP的Trait机制 Trait介绍: 1.自PHP5.4起,PHP实现了一种代码复用的方法,称为trait.2.Trait是为类似PHP的单继承语言二准备的一种代码复用机制.3.Trait为了减少 ...

  5. Maven插件开发教程收集(待实践)

    官方教程:http://maven.apache.org/plugin-developers/index.html http://blog.csdn.net/csfreebird/article/de ...

  6. webuploader设置timeout

    参考:http://www.codingwhy.com/view/841.html 备注下!

  7. mysql统计功能和数据库information_schema/performance_schema

    1.去重统计数据表行数: select count(distinct col_name) from table_name; 2.统计行数 select count(*) from table_name ...

  8. 数据库官方在线文档列表(mysql, postgreSQL)

    1. mysql http://dev.mysql.com/doc/ 2. postgreSQL https://www.postgresql.org/docs/

  9. Bootstrap全局CSS样式之button和图片

    .btn-default--button的默认样式. .btn-primary--button的首选样式: .btn-success--button的成功样式: .btn-info--button的一 ...

  10. poj 1659 Frogs&#39; Neighborhood 度序列可图化 贪心

    题意: 对一个无向图给出一个度序列,问他是否可简单图化. 分析: 依据Havel定理,直接贪心就可以. 代码: //poj 1659 //sep9 #include <iostream> ...