题目描述:

参考后提交:并查集:

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. ftp的虚拟用户的使用

    虚拟用户原理 因为在linux之下,使用vsftp建立用户之后,默认使用ftp访问的时候,是会访问到对应的用户家目录.如果想多个用户同时访问某一个目录,同时对同一目录下有着不同的权限,比如部分用户只能 ...

  2. Java中的API

    待施工 111 API: Scanner Random String StringBuilder ArrayList 集合详解: 包 import java.util.ArrayList 构造方法pu ...

  3. final关键字和static关键字

    final关键字:最终态--修饰成员变量,成员方法,类 final修饰变量: 基本类型变量:该变量为常量不能被赋值 引用类型变量:该地址不能被概变 地址不能被概变的原因: final Student ...

  4. shiro实现用户踢出功能

    shiro实现用户踢出功能 KickoutSessionControlFilte import java.io.IOException; import java.io.PrintWriter; imp ...

  5. C之输入输出函数(2) -- gets()

    https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/rtref/gets.htm#gets #include <stdio.h ...

  6. cmake build

    { //cmake CMakeLists.txt -G "Visual Studio 15 2017" }

  7. Windows NtQueryInformationProcess()

    { https://www.orcode.com/article/Processes_20126324.html } { 或代码 文章 编程通用 线程,进程及IPC 与NtQueryInformati ...

  8. delphi TTcpClient TTcpServer分析(转)

    delphi TTcpClient TTcpServer分析(转) 只描述windows socket部分. sockets.pas中各个类得继承关系: TBaseSocket | --------- ...

  9. AJAX(包括跨域)post请求封装

    function ajaxPost(dataUrl, parameter, callback, bef_callback, com_callback, err_callback) { $.ajax({ ...

  10. 【Codeforces Round #429 (Div. 2) C】Leha and Function

    [Link]:http://codeforces.com/contest/841/problem/C [Description] [Solution] 看到最大的和最小的对应,第二大的和第二小的对应. ...