leetcode-685-冗余连接②
题目描述:
参考后提交:并查集:
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-冗余连接②的更多相关文章
- Java实现 LeetCode 685 冗余连接 II(并查集+有向图)
685. 冗余连接 II 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着 ...
- Leetcode 684.冗余连接
冗余连接 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, ..., N) 的树及一条附加的边构成.附加的边的两个顶点包含在1到N中间,这条 ...
- Java实现 LeetCode 684 冗余连接(并查集)
684. 冗余连接 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, -, N) 的树及一条附加的边构成.附加的边的两个顶点包含在1到N中间 ...
- LeetCode 85. 冗余连接 II
题目: 在本问题中,有根树指满足以下条件的有向图.该树只有一个根节点,所有其他节点都是该根节点的后继.每一个节点只有一个父节点,除了根节点没有父节点. 输入一个有向图,该图由一个有着N个节点 (节点值 ...
- Leetcode之并查集专题-684. 冗余连接(Redundant Connection)
Leetcode之并查集专题-684. 冗余连接(Redundant Connection) 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2 ...
- [Swift]LeetCode684. 冗余连接 | Redundant Connection
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- openwrt ssh免密登录
1 生成相关秘钥 dropbearkey -t rsa -f id_rsa dropbearkey -y -f id_rsa | grep "^ssh-rsa" >> ...
- python 获取淘宝商品信息
python cookie 获取淘宝商品信息 # //get_goods_from_taobao import requests import re import xlsxwriter cok='' ...
- Java中的Set集合以及HashSet
Set集合: Set继承自Collection,所以没有什么特别的方法. 需要注意的是,Set集合不包含重复元素,我们重点了解Set集合如何保证不包含多余元素. HashSet: HashSet如何保 ...
- loadrunner自定义函数
https://zhangfy068.iteye.com/blog/1614794 Loadruner 有四种实现自定义函数的方式,根据脚本编写方便性进行选择不同的方式. (1)直接引用法: Acti ...
- 【转】java即时消息推送
整个例子的源码下载:http://pan.baidu.com/s/1gfFYSbp 下载服务端jar文件 Comet4J目前仅支持Tomcat6.7版本,根据您所使用的Tomcat版本下载[comet ...
- php-验证码类-PDO类-缩略图类
Verify.class.php 验证码类 <?php class Verify{ const VERIFY_TYPE_NUM=1; const VERIFY_TYPE_EN=2; const ...
- Ext 可编辑的GridPanel
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python之—flask虚拟环境
一.安装: 输入以下命令可以检查系统是否安装了virtualenv : $ virtualenv --version 如果结果显示错误,你就需要安装这个工具. 1.Ubuntu 用户可以使用下述命令安 ...
- mysql笔试题大餐---1、组合查询方式及having
mysql笔试题大餐---1.组合查询方式及having 一.总结 一句话总结: 实践:我之前的mysql真的学的太浅了,这种情况下,依据实践(做题)才是唯一能把它学好的方式 学的暂时够了,以实践而学 ...
- flask json
导入 from flask import Flask,jsonify 1.列表 def index(): arr=['mkdir','md','touch'] return jsonify(arr) ...