C++网络中的关键连接
假设有n台服务器,编号从0到n-1,由服务器到服务器的无向连接构成一个网络,其中connections[i] = [a,b]表示服务器a和b之间的连接。所有服务器都直接或通过其他服务器连接。关键连接是指如果移除该连接,则会使某些服务器无法到达其他服务器的连接。我们需要找到所有关键连接。
例如,如果输入n = 4,connection = [[0,1],[1,2],[2,0],[1,3]],

则输出为[[1,3]]
为了解决这个问题,我们将遵循以下步骤:
定义一个集合
定义一个数组disc
定义一个数组low
定义一个二维数组ret
定义一个函数dfs(),它将接收节点、父节点、一个二维数组graph作为参数。
如果节点已在visited中:
返回
将节点插入visited
disc[node] := time
low[node] := time
(time加1)
对于graph[node]中的所有元素x
如果x等于父节点:
忽略以下部分,跳到下一个迭代
如果x不在visited中:
dfs(x, node, graph)
low[node] := low[node]和low[x]的最小值
如果disc[node] < low[x]:
将{node, x}插入ret的末尾
否则
low[node] := low[node]和disc[x]的最小值
在主方法中,执行以下操作:
将disc的大小设置为n + 1
将low的大小设置为n + 1
time := 0
定义一个大小为graph n + 1的列表数组
初始化i := 0,当i < v的大小,更新(i加1),执行:
u := v[i, 0]
w := v[i, 1]
将w插入graph[u]的末尾
将u插入graph[w]的末尾
dfs(0, -1, graph)
返回ret
让我们看下面的实现来更好地理解:
示例
#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<vector<auto> > v){
cout << "[";
for(int i = 0; i<v.size(); i++){
cout << "[";
for(int j = 0; j <v[i].size(); j++){
cout << v[i][j] << ", ";
}
cout << "],";
}
cout << "]"<<endl;
}
class Solution {
public:
set<int> visited;
vector<int> disc;
vector<int> low;
int time;
vector<vector<int> > ret;
void dfs(int node, int par, vector<int> graph[]) {
if (visited.count(node))
return;
visited.insert(node);
disc[node] = low[node] = time;
time++;
for (int x : graph[node]) {
if (x == par)
continue;
if (!visited.count(x)) {
dfs(x, node, graph);
low[node] = min(low[node], low[x]);
if (disc[node] < low[x]) {
ret.push_back({ node, x });
}
} else{
low[node] = min(low[node], disc[x]);
}
}
}
vector<vector<int> > criticalConnections(int n, vector<vector<int> >& v) {
disc.resize(n + 1);
low.resize(n + 1);
time = 0;
vector<int> graph[n + 1];
for (int i = 0; i < v.size(); i++) {
int u = v[i][0];
int w = v[i][1];
graph[u].push_back(w);
graph[w].push_back(u);
}
dfs(0, -1, graph);
return ret;
}
};
main(){
Solution ob;
vector<vector<int>> v = {{0,1},{1,2},{2,0},{1,3}};
print_vector(ob.criticalConnections(4,v));
}输入
4, {{0,1},{1,2},{2,0},{1,3}}输出
[[1, 3, ],]
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP