tarjan算法

喜欢而已 提交于 2019-11-30 23:09:56

还是有点晦涩难懂

 1 vector<int> low;
 2 vector<int> dfn;
 3 vector<int> father;
 4 vector<vector<int>> g;
 5 int time = 0;
 6 void tarjan(int i, int ifather)
 7 {
 8     father[i] = ifather;
 9     low[i] = time;
10     dfn[i] = time;
11     time++;
12     for (int j = 0; j < g[i].size(); j++)
13     {
14         if (g[i][j] == father[i])
15             continue;
16         else if (dfn[g[i][j]] == -1)
17         {
18             tarjan(g[i][j], i);
19             low[i] = min(low[i], low[g[i][j]]);
20         }
21         else
22             low[i] = min(low[i], dfn[g[i][j]]);
23     }
24 }
25 vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
26     vector<int> use(n, -1);
27     vector<vector<int>> g1(n, vector<int>());
28     g = g1;
29     low = use;
30     dfn = use;
31     father = use;
32     for (auto e : connections)
33     {
34         g[e[0]].push_back(e[1]);
35         g[e[1]].push_back(e[0]);
36     }
37     tarjan(0, -1);
38     vector<vector<int>> ans;
39     for (int i = 0; i < n; i++)
40     {
41         int f = father[i];
42         if (father[i] >= 0 && low[i] > dfn[f])
43         {
44             vector<int> temp;
45             temp.push_back(i);
46             temp.push_back(f);
47             ans.push_back(temp);
48         }
49     }
50     return ans;
51 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!