-
Notifications
You must be signed in to change notification settings - Fork 1
/
1192.查找集群内的关键连接.cpp
77 lines (59 loc) · 1.54 KB
/
1192.查找集群内的关键连接.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "s.h"
/*
* @lc app=leetcode.cn id=1192 lang=cpp
*
* [1192] 查找集群内的关键连接
*/
// @lc code=start
class Solution {
public:
vector<int> low, dfn, vis;
vector<vector<int>> ans;
vector<vector<int>> adj;
int now;
int n;
void tarjan(int cur, int fa){// 找环
now++;
vis[cur] = 1;
low[cur]=dfn[cur]=now;
for(auto it: adj[cur]){
if(it==fa){
continue;
}
if(!vis[it]){
tarjan(it, cur);
low[cur] = min(low[it], low[cur]);// 逆回来,设置自己的low为子树的low
if(dfn[cur] < low[it]){ // 如果子树没有碰到自己或之前的节点,那么这个路径肯定是关键路径
ans.push_back({cur,it});
}
}else{
low[cur] = min(low[cur], dfn[it]); // 顺向碰到之前访问过的,时间更小的地方,修改自己的low
}
}
}
vector<vector<int>> criticalConnections(int _n,const vector<vector<int>>& connections) {
n = _n;
adj.resize(n);
for(auto i: connections){
adj[i[0]].push_back(i[1]);
adj[i[1]].push_back(i[0]);
}
low.resize(n);
dfn.resize(n);
vis.resize(n);
now = 0;
tarjan(0,-1);
return ans;
}
};
// @lc code=end
int main(){
Solution s;
print2d(s.criticalConnections(4,{
{0,1},
{1,2},
{2,0},
{1,3}
}));
return 0;
}