POJ-3895(vector数组+dfs)

【题目描述】

求一个图中最大环的点个数

vector<int> g[4445];
int visited[4445];
int num[4445];
int ans;
void dfs(int v, int t)
{
	num[v] = t;
	visited[v] = 1;
	int l = g[v].size(), i;
	for (i = 0; i < l; ++i) {
		if (visited[g[v][i]]) {
			ans = MY_MAX(ans, num[v] - num[g[v][i]] + 1);
		} else {
			dfs(g[v][i], t + 1);
		}
	}
}
int main ()
{
	int t;
	scanf("%d", &t);
	while (t--) {
		ans = 0;
		memset(visited, 0, sizeof(visited));
		memset(num, 0, sizeof(num));
		int a, b, i, j, x, y;
		scanf("%d%d", &a, &b);
		for (i = 1; i <= a; ++i) g[i].clear();
		for (i = 0; i < b; ++i) {
			scanf("%d%d", &x, &y);
			g[x].push_back(y);
			g[y].push_back(x);
		}
		for (i = 1; i <= a; ++i) {
			if (visited[i] == 0) {
				dfs(i, 1);
			}
		}
		if (ans > 2) printf("%d\n", ans);
		else printf("0\n");
	}
    return 0;
}


你可能感兴趣的:(POJ-3895(vector数组+dfs))