这个题用光搜做,从网上看的,我一开始以为是深搜,写半天没写出来。最后从网上看要用广搜做,想了半天,还觉得应该用深搜,无奈的我回到我的床上,躺下吃了两粒花生米,仔细思考了一下,就想出来广搜怎么做了!沉下心来,坐在电脑面前没有躺在床上思考的更犀利。我记得以前也有一个模拟链表的题也是在床上临睡前想出来的。
用deep[]来记录层数,这个题感觉不是太难(写完之后。)
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=10011;
int N,M;
int level;
int book[maxn];
int deep[maxn];
vector<int> vec[maxn];
int bfs(int n){
int level=1;
queue<int> q;
q.push(n);
book[n]=1;
deep[n]=0;
while(!q.empty()){
int temp=q.front();
q.pop();
for(int i=0;i<vec[temp].size();i++){
if(book[vec[temp][i]]==0){
book[vec[temp][i]]=1;
deep[vec[temp][i]]=deep[temp]+1;
q.push(vec[temp][i]);
}
}
}
int cnt=0;
for(int i=1;i<=N;i++){
if(deep[i]<=6)
cnt++;
// cout<<deep[i]<<" **";
}
return cnt;
}
int main(){
int a,b;
scanf("%d%d",&N,&M);
for(int i=1;i<=M;i++){
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
}
for(int i=1;i<=N;i++){
memset(book,0,sizeof(book));
memset(deep,INF,sizeof(deep));
printf("%d: %.2lf",i,(double)bfs(i)/(double)N*100);
printf("%%\n");
}
return 0;
}