1 Star 0 Fork 0

林进源 / 我错了

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
7-2 六度空间 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
林进源 提交于 2021-05-24 21:16 . add 7-2 六度空间.
#include <iostream>
#include <string>
#include <queue>
using namespace std;
static int map[1002][1002];//定义矩阵,static省去初始化
int BFS(int i);
int n, e;
int main()
{
cin >> n >> e;
int i;
for (i = 1; i <= e; i++)
{
int a, b;
cin >> a >> b;
map[a][b] = 1;
map[b][a] = 1;
}
int count=0;
double num;
for (i = 1; i <= n; i++)
{
count = BFS(i);//计算数量
num = count * 100.0 / n;
printf("%d: %.2f%\n", i, num);
}
return 0;
}
int BFS(int i)//广度遍历
{
bool visited[10002] = {false};//定义visited来储存已访问的结点
queue<int>q;
q.push(i);
visited[i] = 1;
int level = 0;//层数
int count = 1;
int last =i;
int tail;
while (!q.empty())
{
i = q.front();
q.pop();
for (int j = 1; j <= n; j++)
{
if (!visited[j] && map[i][j] == 1)
{
q.push(j);
count++;
visited[j] = true;
tail = j;
}
}
if (last == i)//判断是否为该层最后一个
{
level++;
last = tail;//移动到下一层
}
if (level == 6)
{
return count;
}
}
return count;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/lin-jinyuan/i-was-wrong.git
git@gitee.com:lin-jinyuan/i-was-wrong.git
lin-jinyuan
i-was-wrong
我错了
master

搜索帮助