题目描述:

参考后提交:并查集:

class Solution:
def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:
def find(f,x):
f.setdefault(x,x)
if f[x] != x:
f[x] = find(f,f[x])
return f[x]
def cycle(graph):
f = {}
for x,y in graph:
if find(f,x) == find(f,y):
return True
else:
f[find(f,y)] = find(f,x)
indegree = {i:0 for i in range(1,len(edges)+1)}
tmp = 0
for i,j in edges:
indegree[j] += 1
if indegree[j] == 2:
tmp = j
break
if tmp == 0:
f = {}
for x,y in edges:
if find(f,x) == find(f,y):
return [x,y]
else:
f[find(f,y)] = find(f,x)
else:
for x,y in edges[::-1]:
if y == tmp:
if not cycle(edges[:edges.index([x,y])]+edges[edges.index([x,y])+1:]) :
return [x,y]

leetcode-685-冗余连接②的更多相关文章

  1. Java实现 LeetCode 685 冗余连接 II(并查集+有向图)

    685. 冗余连接 II 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着 ...

  2. Leetcode 684.冗余连接

    冗余连接 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, ..., N) 的树及一条附加的边构成.附加的边的两个顶点包含在1到N中间,这条 ...

  3. Java实现 LeetCode 684 冗余连接(并查集)

    684. 冗余连接 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, -, N) 的树及一条附加的边构成.附加的边的两个顶点包含在1到N中间 ...

  4. LeetCode 85. 冗余连接 II

    题目: 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着N个节点 (节点值 ...

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

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

  6. [Swift]LeetCode684. 冗余连接 | 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 冗余的连接之 II

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

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

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

  10. [Swift]LeetCode685. 冗余连接 II | Redundant Connection II

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

随机推荐

  1. RMI远端方法调用

    一.RMI介绍 RMI(Remote Method Invocation),RMI是分布式对象软件包,它简化了在多台计算机上的JAVA应用之间的通信.必须在jdk1.1以上,RMI用到的类:java. ...

  2. 前端学习(十)初识js(笔记)

    js事件(公有属性) onclick=""  当点击...时! onmouseover="" 当鼠标移入...时!onmouseout="" ...

  3. Spring的PropertyPlaceholderConfigurer应用(转)

    转自:http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html Spring 利用PropertyPlaceholderConfigu ...

  4. 【Luogu】【关卡2-15】动态规划的背包问题(2017年10月)【还差一道题】

    任务说明:这是最基础的动态规划.不过如果是第一次接触会有些难以理解.加油闯过这个坎. 01背包二维数组优化成滚动数组的时候有坑有坑有坑!!!必须要downto,downto,downto 情景和代码见 ...

  5. react 的生命周期函数

    生命周期函数: 是指在某一时刻组件自动执行 的函数 初始化: 设置props和state mounting: componentWillMount 在组件即将被挂载到页面的时候自动执行 render ...

  6. spark window本地运行wordcount错误

    在运行本地运行spark或者hadoop代码时可能会遇到一下三种问题   1.Exception in thread "main" java.lang.UnsatisfiedLin ...

  7. SQL的多表查询(Navicat)

    -- 部门表 CREATE TABLE dept ( id INT PRIMARY KEY PRIMARY KEY, -- 部门id dname VARCHAR(50), -- 部门名称 loc VA ...

  8. openFrameworks Download

    { https://openframeworks.cc/zh_cn//download/ } 0.10.1 是最新发布的版本. 这个版本是修改了一些BUG的小版本,与版本 0.10.1100%兼容而且 ...

  9. python培训拾遗

    20140421 1. 三大利器: dir----列出所有内部方法 a=1 dir(a) 可以列出所有内部方法,就是带两个下划线的:带一个下划线的是普通方法 help---查看帮助 help(a.bi ...

  10. 在Windows Server2016中安装SQL Server2016(转)

    在Windows Server2016中安装SQL Server2016(转) 转自: http://blog.csdn.net/yenange/article/details/52980135 参考 ...