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. SAP 常用增强记录文档

    转自:http://blog.csdn.net/budaha 20170215需要一个PR 修改保存时候的增强,目的是同步PR的处理状态 EBAN-STATU 到一个自建表ZTPRTOPO,记得有个P ...

  2. java 内存简介

    java程序对内存分配的方式一般有三种: (1) 从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量. (2) 在栈上创建. 在执行函数时,函数内局 ...

  3. 表单提交数据量大于2m,java 后台接受不到表单传递过来的数据

    一般来说 post请求提交的数据无大小限制,但是tomcat 设置默认的表单传输数据大小不能2m,这时候当数据大于2m后台接收达不到表单的数据,需要修改tomcat的server.xml的的maxPo ...

  4. 线程之间的通信socketpair【学习笔记】【原创】

    平台信息:内核:linux3.1.0系统:android5.0平台:tiny4412 作者:庄泽彬(欢迎转载,请注明作者) 说明: 韦老师的安卓视频学习笔记 一.在一个进程中多个线程如何进行通信,主要 ...

  5. 获取WiFi MAC地址总结【转】

    本文转载自:http://blog.csdn.net/crazyman2010/article/details/50464256 今天对MAC地址的获取做了一些学习,目前网上获取MAC地址的方法主要如 ...

  6. apply current folder view to all folders

    https://www.tenforums.com/tutorials/35093-apply-folder-view-all-folders-same-type-windows-10-a.html ...

  7. frameset 框架整体退出登录的问题

    1 设置其他的页面都验证session,如果session不存在就跳转到 Login 页: 2 Login中添加下面的js代码: <script language="JavaScrip ...

  8. mac多线程下载神器

    本文参考:https://blog.csdn.net/orangleliu/article/details/46834429 神器:axel 安装(已经安装homebrew前提下,没有请参考:http ...

  9. Identity 更改密码的几种方式

    1.先使用UserManager.RemovePassword(UserID),清空密码,再使用UserMnager.AddPassword(UserId,newPassword)增加新密码. [Ht ...

  10. Linux后门入侵检测工具,附bash漏洞解决方法

    一.rootkit简介 rootkit是Linux平台下最常见的一种木马后门工具,它主要通过替换系统文件来达到入侵和和隐蔽的目的,这种木马比普通木马后门更加危险和隐蔽,普通的检测工具和检查手段很难发现 ...