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. Struts2逻辑视图与视图资源

  2. RobotFrameWork--selenium2模拟chrome的user agent

    ${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver ${opt ...

  3. vue 基础知识随笔

    在vue2.0中一个vue实例的生命周期中已经没有ready()了,在vue1.0中才有ready();在vue2.0中立即执行函数使用mounted v-for 参数顺序更新: 数组中使用(valu ...

  4. ContextLoaderListener与RequestContextListener配置问题

    转自:https://blog.csdn.net/yyqhwr/article/details/83381447 SSH2.SSM等web应用开发框架的配置过程中,因为都要用到spring,所以,往往 ...

  5. 03_隐式意图打开activity

    想让第一个activity把第二个activity打开的话,在清单文件里面声明一下并且 右键Debug As Android Application居然没有报错 mimeType  讲HTML的时候就 ...

  6. Gym 100531J Joy of Flight (几何)

    题意:你从开始坐标到末尾坐标,要经过 k 秒,然后给你每秒的风向,和飞机的最大速度,问能不能从开始到末尾. 析:首先这个风向是不确定的,所以我们先排除风向的影响,然后算出,静风是的最小速度,如果这都大 ...

  7. HDU1180:诡异的楼梯

    传送门 题意 迷宫搜索 分析 这题写起来挺简单的,锻炼搜索基本功,一开始用记忆化搜索TLE了,改用访问标记,0ms过了,用优先队列保证终点最快达到,我会在代码中提供一些强力数据 trick 1.遇到梯 ...

  8. ThinkPHP3.2.3学习笔记2---模型

    一.模型实例化1.直接实例化可以和实例化其他类库一样实例化模型类,例如:$User = new \Home\Model\UserModel();$Info = new \Admin\Model\Inf ...

  9. bzoj 2194: 快速傅立叶之二【NTT】

    看别的blog好像我用了比较麻烦的方法-- (以下的n都--过 \[ c[i]=\sum_{j=i}^{n}a[i]*b[j-i] \] 设j=i+j \[ c[i]=\sum_{j=0}^{n-i} ...

  10. SAMP论文学习

    SAMP:稀疏度自适应匹配追踪 实际应用中信号通常是可压缩的而不一定为稀疏的,而且稀疏信号的稀疏度我们通常也会不了解的.论文中提到过高或者过低估计了信号的稀疏度,都会对信号的重构造成影响.如果过低估计 ...