https://leetcode.com/problems/redundant-connection/

一个无向图,n个顶点有n条边,输出一条可以删除的边,删除后使得图成为一棵树。可以使用并查集解决。

class Solution
{
public:
int father[];
void initfather()
{
for(int i=; i<=; i++)
father[i]=i;
} int findFather(int x)
{
if(father[x]!=x)
father[x] = findFather(father[x]);
return father[x];
} void Merge(int x1, int x2)
{
int father_x1 = findFather(x1);
int father_x2 = findFather(x2);
if(father_x1 != father_x2)
father[father_x2] = father_x1;
} vector<int> findRedundantConnection(vector<vector<int>>& edges)
{
initfather();
vector<int> res;
for(auto edge:edges)
{
int u = edge[];
int v = edge[];
if(findFather(u) == findFather(v))
{
res=edge;
break;
}
else
Merge(u,v);
}
return res;
}
};

leetcode_684. Redundant Connection的更多相关文章

  1. [LeetCode] Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  2. [LeetCode] Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  3. [Swift]LeetCode684. 冗余连接 | Redundant Connection

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  4. LN : leetcode 684 Redundant Connection

    lc 684 Redundant Connection 684 Redundant Connection In this problem, a tree is an undirected graph ...

  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] 684. Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

  7. LeetCode 685. Redundant Connection II

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

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

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

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

随机推荐

  1. 基于struts环境下的jquery easyui环境搭建

    下载地址: http://download.csdn.net/detail/cyberzhaohy/7348451 加入了json包:jackson-all-1.8.5.jar,项目结构例如以下: 測 ...

  2. [MAC] Load Crypto.Cipher.ARC4 Failed, Use Pure Python Instead.

    MAC启动GoAgent,出现Load Crypto.Cipher.ARC4 Failed, Use Pure Python Instead. 解决方法:为Python安装pycrypto,可通过Py ...

  3. YTU 2440: C++习题 复数类--重载运算符+,-,*,/

    2440: C++习题 复数类--重载运算符+,-,*,/ 时间限制: 1 Sec  内存限制: 128 MB 提交: 1189  解决: 774 题目描述 定义一个复数类Complex,重载运算符& ...

  4. 【CJOJ】Contest4 - A+B Series

    Position:http://oj.changjun.com.cn/contest.php?cid=4 A经典题目 // <A.cpp> - Sun Oct 9 15:28:01 201 ...

  5. 将多个jar合并为一个jar

    有时候,我们需要将多个jar合并为一个jar,ant提供了这样的一个功能 build.xml文件如下 执行以上任务会将 当前目录下的 mysql.jar,commons-io.jar合并为一个 all ...

  6. CAShapeLayer和贝塞尔曲线配合使用

    前言 CAShapeLayer继承自CALayer,因此,可使用CALayer的所有属性.但是,CAShapeLayer需要和贝塞尔曲线配合使用才有意义. 关于UIBezierPath,请阅读文章:i ...

  7. AutoIT: 句柄的妙用

    句柄是独一无二的,很多时候,Title,Command都可以用句柄来代替.以下写法是能够起一样的作用. $handle= WinGetHandle("autoit cn") $ct ...

  8. 初识Spring Boot框架和快速入门

    前面的铺垫文章已经连着写了六篇了,主要是介绍了spring和SpringMVC框架,小伙伴们在学习的过程中大概也发现了这两个框架需要我们手动配置的地方非常多,不过做JavaEE开发的小伙伴们肯定也听说 ...

  9. OC:基础总结

    OC面向对象的编程语言思想 类与对象.继承与实例化.属性点语法.内存管理.字符串.可见度. 类是一组具有相同特征和行为的事物的抽象 OC的与C相比所具有的新的特点: 定义新的类.类的实例和方法.方法的 ...

  10. javascript判断页面是否在移动设备上打开

    var ua = navigator.userAgent; var ipad = ua.match(/(iPad).*OS\s([\d_]+)/), isIphone =!ipad && ...