LeetCode 1192. Critical Connections in a Network

Stella981
• 阅读 556

原题链接在这里:https://leetcode.com/problems/critical-connections-in-a-network/

题目:

There are n servers numbered from 0 to n-1 connected by undirected server-to-server connections forming a network where connections[i] = [a, b] represents a connection between servers a and b. Any server can reach any other server directly or indirectly through the network.

critical connection is a connection that, if removed, will make some server unable to reach some other server.

Return all critical connections in the network in any order.

Example 1:

LeetCode 1192. Critical Connections in a Network

Input: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
Output: [[1,3]]
Explanation: [[3,1]] is also accepted.

Constraints:

  • 1 <= n <= 10^5
  • n-1 <= connections.length <= 10^5
  • connections[i][0] != connections[i][1]
  • There are no repeated connections.

题解:

How to find the bridge of connected graph, we need to find the edge that is not in the cycle.

Then how to find the edge that is not in the cycle. Iterate from one starting node, mark it as visited and the time it has been visited.

Perform DFS for its neighbors, and track the lowest node that this node could reach out to.

When its id < lowest node it could reach out to, then (u, v) is a critical edge.

When meeting a node v that has been visited before, update current node u low with ids[v].

When backtracking, update current node u low with DFS next node v low[v].

Thus DFS state needs to know current node, low array, ids array, graph, res and parent node.

Since this is undirected graph, we want parent to avoid DFS to parent.

Time Complexity: (n + connections.size()). DFS takes O(V + E). each node is only traversed no more than 2 times.

Space: O(v).

AC Java: 

 1 class Solution {
 2     int id = 0;
 3     
 4     public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) {
 5         List<List<Integer>> res = new ArrayList<>();
 6         if(n < 1){
 7             return res;
 8         }
 9         
10         List<Integer> [] graph = new ArrayList[n];
11         for(int i = 0; i < n; i++){
12             graph[i] = new ArrayList<>();
13         }
14         
15         for(List<Integer> e : connections){
16             graph[e.get(0)].add(e.get(1));
17             graph[e.get(1)].add(e.get(0));
18         }
19         
20         int [] ids = new int[n];
21         Arrays.fill(ids, -1);
22         int [] low = new int[n];
23         
24         for(int i = 0; i < n; i++){
25             if(ids[i] == -1){
26                 dfs(i, low, ids, graph, res, -1);
27             }
28         }
29         
30         return res;
31     }
32     
33     private void dfs(int u, int [] low, int [] ids, List<Integer> [] graph, List<List<Integer>> res, int parent){
34         ids[u] = low[u] = ++id;
35         List<Integer> neibors = graph[u];
36         
37         for(int v : neibors){
38             if(v == parent){
39                 continue;
40             }
41             
42             if(ids[v] == -1){
43                 dfs(v, low, ids, graph, res, u);
44                 low[u] = Math.min(low[u], low[v]);
45                 
46                 if(low[v] > ids[u]){
47                     res.add(Arrays.asList(u, v));
48                 }
49             }else{
50                 low[u] = Math.min(low[u], ids[v]);
51             }
52         }
53     }
54 }
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
LeetCode——295. Find Median from Data Stream
一.题目链接:  https://leetcode.com/problems/findmedianfromdatastream(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fleetcode.com%2Fproblems%2Ffindmedianfromdatast
Stella981 Stella981
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Easter79 Easter79
3年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这