每日算法系列【LeetCode 685】冗余连接 II

简介: 在本问题中,有根树指满足以下条件的有向图。该树只有一个根节点,所有其他节点都是该根节点的后继。每一个节点只有一个父节点,除了根节点没有父节点。

题目描述


在本问题中,有根树指满足以下条件的有向图。该树只有一个根节点,所有其他节点都是该根节点的后继。每一个节点只有一个父节点,除了根节点没有父节点。

image.png

示例1

输入:
[[1,2], [1,3], [2,3]]
输出:
[2,3]
解释:
1 / \v   v2-->3

示例2

输入:
[[1,2], [2,3], [3,4], [4,1], [1,5]]
输出:
[4,1]
解释:
5 <- 1 -> 2     ^    |     |    v     4 <- 3

提示

  • 输入的二维数组大小在  到 。
  • 二维数组中的整数在  到  之间,其中  是输入数组的大小。

题解


这题是上一道题 每日算法系列【LeetCode 684】冗余连接 的进阶版,区别就是无向图变成了有向图。


image.png

代码


c++

classSolution {
public: 
staticconstintN=1010;  
intf[N], degree[N];  
intn;
vector<int>findRedundantDirectedConnection(vector<vector<int>>&edges) {   
n=edges.size();    
memset(degree, 0, sizeofdegree); 
for (autoe : edges) ++degree[e[1]];    
for (inti=n-1; i>=0; --i) {   
if (degree[edges[i][1]] ==2&&!wrongEdge(edges, 
i).size()) {                returnedges[i];   
                                                                  }        }  
returnwrongEdge(edges, n);
    }
vector<int>wrongEdge(vector<vector<int>>&edges, intexcept) {        init();   
for (inti=0; i<n; ++i) {  
if (i==except) continue;   
intu=edges[i][0], v=edges[i][1];   
if (same(u, v)) returnedges[i]; 
join(u, v);     
                                                                   }        return {}; 
                                                                  }
voidinit() {   
for (inti=1; i<=n; ++i) {    
f[i] =i;     
        }  
    }
intfind(intu) {  
returnu==f[u] ?u : f[u]=find(f[u]); 
    }
voidjoin(intu, intv) {  
u=find(u);     
v=find(v);    
if (u==v) return; 
f[v] =u;  
    }
boolsame(intu, intv) {   
u=find(u);      
v=find(v);   
returnu==v;   
    }
};

python

classSolution:  
deffindRedundantDirectedConnection(self, edges: 
List[List[int]]) ->List[int]:   
self.n=len(edges)      
degree= [0] * (self.n+1)     
foru, vinedges:     
degree[v] +=1foru, vinedges[::-1]:  
ifdegree[v] ==2andlen(self.wrongEdge(edges, [u,
v])) ==0:  
return [u, v]   
returnself.wrongEdge(edges, [])        
defwrongEdge(self, edges, ex):   
self.f= [iforiinrange(self.n+1)]  
foru, vinedges:        
if [u, v] ==ex:      
continueifself.same(u, v): 
return [u, v]  
self.join(u, v)   
return []      
deffind(self, u):   
ifu==self.f[u]:   
returnuself.f[u] =self.find(self.f[u])  
returnself.f[u]
defjoin(self, u, v):  
u, v=self.find(u), self.find(v) 
ifu==v:     
returnself.f[v] =udefsame(self, u, v):  
u, v=self.find(u), self.find(v) 
returnu==v


image.png

作者简介:godweiyang知乎同名华东师范大学计算机系硕士在读,方向自然语言处理与深度学习喜欢与人分享技术与知识,期待与你的进一步交流~


相关文章
|
18天前
|
存储 算法 JavaScript
怎么刷算法,leetcode上有哪些经典题目
怎么刷算法,leetcode上有哪些经典题目
18 0
|
18天前
|
算法 Java
[Java·算法·中等] LeetCode15. 三数之和
[Java·算法·中等] LeetCode15. 三数之和
35 0
|
10天前
|
算法
【优选算法】——Leetcode——LCR 179. 查找总价格为目标值的两个商品
【优选算法】——Leetcode——LCR 179. 查找总价格为目标值的两个商品
|
10天前
|
算法
【优选算法】——Leetcode——611. 有效三角形的个数
【优选算法】——Leetcode——611. 有效三角形的个数
|
10天前
|
算法 容器
【优选算法】—Leetcode—11—— 盛最多水的容器
【优选算法】—Leetcode—11—— 盛最多水的容器
|
10天前
|
算法
【优选算法】——Leetcode——202—— 快乐数
【优选算法】——Leetcode——202—— 快乐数
【优选算法】——Leetcode——202—— 快乐数
|
10天前
|
算法
[优选算法]——双指针——Leetcode——1089. 复写零
[优选算法]——双指针——Leetcode——1089. 复写零
|
10天前
|
算法
【优选算法】——双指针——Leetcode——283.移动零
【优选算法】——双指针——Leetcode——283.移动零
|
18天前
|
算法 Java vr&ar
保持无损连接和函数依赖的3NF合成算法(详细简介)期末必备
保持无损连接和函数依赖的3NF合成算法(详细简介)期末必备
17 0
|
18天前
|
算法 vr&ar
保持无损连接的BCNF分解算法
保持无损连接的BCNF分解算法
20 1