思想很简单,就是有dfs遍历图并且用一个数组记录所走的长度。当来到一个点是已经访问过的,则肯定形成一个环,更新环的长度,直到所有点访问完为止。
代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define _Clr(x, y) memset(x, y, sizeof(x)) 5 #define INF 0x3f3f3f3f 6 #define N 5000 7 using namespace std; 8 9 int head[N], tot; 10 struct Edge 11 {