什么是强连通图
任意两个顶点都是强连通,就是强连通了
tarjan
利用dfs递归的思想,递归的时候记下这个顶点是第几次访问并进栈x,在回归的时候记下当前节点的子节点中离初始节点最近的点y,如果x==y,那么出栈直到当前节点,出来的节点就是一个联通分量了,如果不是全体节点,那么,就不是强连通
测试数据:
5
7
0 1
1 2
1 3
2 3
3 0
3 4
4 0
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
public class Main {
static int[][] data = new int[20][20];
static int[] max = new int[10];
static int[] min = new int[10];
static int depth = 0;
static Stack<Integer> stack = new Stack<Integer>();
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int v = in.nextInt();
int e = in.nextInt();
int x, y;
for (int i = 0; i < e; i++) {
x = in.nextInt();
y = in.nextInt();
data[x][y] = 1;
}
if(dfs(0,v)) System.out.print("yes");
else System.out.print("no");
}
static boolean dfs(int i, int n) {
stack.push(i);
max[i] = min[i] = ++depth;
for (int j = 0; j < n; j++) {
if (data[i][j] == 1) {
if (max[j] == 0) {
if (!dfs(j, n))
return false;
min[i] = min(min[i], min[j]);
} else if (stack.contains(j)) {
min[i] = min(min[i], min[j]);
}
}
}
if (max[i] == min[i]) {
while (stack.pop() != i);
;
if (!stack.isEmpty())
return false;
}
return true;
}
static int min(int a, int b) {
return a < b ? a : b;
}
}

本文深入探讨了强连通图的概念及其在图论中的重要性,并详细介绍了使用Tarjan算法进行强连通分量查找的过程。通过具体实例和Java代码实现,读者可以了解到如何判断一个图是否为强连通图。
1982

被折叠的 条评论
为什么被折叠?



