-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.3(2).cpp
80 lines (74 loc) · 1.71 KB
/
6.3(2).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <queue>
using namespace std;
#define maxsize 1005
typedef struct edge{
int data;
edge* next;
}edge;
typedef struct vertix{
edge* firstedge;
}vertix;
typedef struct graph{
int vertexnum,edgenum;
vertix vertixs[maxsize];
}graph;
void SixDegree_BFS(graph g,int start);
int main(){
//初始化
graph g;
cin>>g.vertexnum>>g.edgenum;
for(int i=0;i<g.vertexnum;i++)
g.vertixs[i].firstedge=NULL;
for(int i=0;i<g.edgenum;i++){
int x,y;
cin>>x>>y;
edge* s=(edge*)malloc(sizeof(edge));
s->data=y-1;
s->next=g.vertixs[x-1].firstedge;
g.vertixs[x-1].firstedge=s;;
edge* t=(edge*)malloc(sizeof(edge));
t->data=x-1;
t->next=g.vertixs[y-1].firstedge;
g.vertixs[y-1].firstedge=t;
}
//开始计算
for(int i=0;i<g.vertexnum;i++){
SixDegree_BFS(g,i);
}
return 0;
}
void SixDegree_BFS(graph g,int start){
//初始化
int visitnum=1;
int visited[g.vertexnum];
for(int i=0;i<g.vertexnum;i++)
visited[i]=0;
visited[start]=1;
int level=0;
int tail=start;
int last=start;
queue<int> q;
q.push(start);
while(!q.empty()){
int s=q.front();
q.pop();
edge* m=g.vertixs[s].firstedge;
while(m){
if(visited[m->data]==0){
visited[m->data]=1;
visitnum++;
tail=m->data;
q.push(m->data);
}
m=m->next;
}
if(s==last){
level++;
last=tail;
}
if(level==6) break;
}
double percent=100.0*visitnum/g.vertexnum;
printf("%d: %.2lf\%\n",start+1,percent);
}