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. p_CreateAuditEntry

    如果你能搜到我这篇博客,相信你导遇到的了和我一样在导入CRM组织时遇到了类似的错误.这个错误我查资料可以通过CRM升级来解决参考下面连接: https://support.microsoft.com/ ...

  2. solr 命令

    本文为转载内容:源地址:http://blog.csdn.net/matthewei6/article/details/50620600 查看帮助 bin/solr -help            ...

  3. POJ3164 Command Network —— 最小树形图

    题目链接:https://vjudge.net/problem/POJ-3164 Command Network Time Limit: 1000MS   Memory Limit: 131072K ...

  4. ruby gem的安装步骤

    第一步:下载安装文件 官网下载地址:http://rubyinstaller.org/downloads 第二步: 双击安装 在安装的时候,请勾选Add Ruby executables to you ...

  5. STM32:获取复位源,软件复位

    RCC CSR寄存器会存储复位标示,可通过它来知道复位原因,来源: if(RCC_GetFlagStatus(RCC_FLAG_PINRST)) printf("PINRST\r\n&quo ...

  6. ES6躬行记(23)——Promise的静态方法和应用

    一.静态方法 Promise有四个静态方法,分别是resolve().reject().all()和race(),本节将着重分析这几个方法的功能和特点. 1)Promise.resolve() 此方法 ...

  7. bzoj 1609[Usaco2008 Feb]Eating Together麻烦的聚餐【dp】

    设up[i][j]为第i位升序为j的最小修改数,down为降序 #include<iostream> #include<stdio.h> using namespace std ...

  8. bzoj 1913: [Apio2010]signaling 信号覆盖【旋转卡壳(?)】

    参考:https://blog.csdn.net/qpswwww/article/details/45334033 讲的很清楚 做法比较像旋转卡壳但是具体是不是我也不清楚.. 首先知道只要求出每种方案 ...

  9. Word排版技巧

    点击打开链接 # 整体布局 ## 页面布局 如果是新建一个Word文件,这里「页面布局」一般不用设置了: 文字方向:从左到右: 页边距:普通(日常使用建议用适中或窄,节约用纸,提交的论文报告什么才用普 ...

  10. Increasing Sequence CodeForces - 11A

    Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记 ...