5-7 六度空间 (30分)

#include <iostream>
#include <vector>
#include <queue>
#include <string.h>

using namespace std;

vector<int> v[10010];
queue<int> q;
int vis[10010], level, bfscount;

void bfs()
{
	int qsize = q.size(), vsize, cur, i, next;
	bfscount += qsize;

	while(qsize--)
	{
		cur = q.front();
		q.pop();

		vsize = v[cur].size();
		for(i = 0; i < vsize; i++)
		{
			next = v[cur][i];
			if(vis[next] == 0)
			{
				vis[next] = 1;
				q.push(next);
			}
		}
	}

	qsize = q.size();
	if(qsize > 0 && level < 6)
	{
		level++;
		bfs();
	}
}

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);

	int i, a, b;
	for(i = 1; i <= m; i++)
	{
		scanf("%d%d", &a, &b);
		
		v[a].push_back(b);
		v[b].push_back(a);
	}

	for(i = 1; i <= n; i++)
	{
		level = bfscount = 0;
		memset(vis, 0, sizeof(vis));
		vis[i] = 1;

		while(q.size() > 0)
		{
			q.pop();
		}
		q.push(i);

		bfs();
		printf("%d: %.2lf%%\n", i, bfscount * 1.0 / n * 100);
	}

	system("pause");
	return 0;
}

 

posted on 2025-11-29 14:57  王景迁  阅读(0)  评论(0)    收藏  举报

导航