赞
踩
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define MAXN 10005
-
- typedef struct VNode* Vertex;
- struct VNode {
- Vertex Next;
- int V;
- };
-
- typedef struct LNode {
- Vertex FirstEdge;
- }List[MAXN];
-
- typedef struct GNode* Graph;
- struct GNode {
- int Nv,Ne;
- List Head;
- };
- Graph G;
- int Visited[MAXN];
-
- void Insert(int v,int w) {
- //无向图双向都要插
- Vertex NewNode = (Vertex)malloc(sizeof(struct VNode));
- NewNode->V = v;
- NewNode->Next = G->Head[w].FirstEdge;
- G->Head[w].FirstEdge = NewNode;
-
- NewNode = (Vertex) malloc(sizeof(struct VNode));
- NewNode->V = w;
- NewNode->Next = G->Head[v].FirstEdge;
- G->Head[v].FirstEdge = NewNode;
- }
- void BFS(int s) {
- int Q[MAXN],front = 0,rear = 0,v,i;
- int tail,last = s,cnt = 0,level = 0;
- Vertex p;
- Q[rear++] = s;
- Visited[s] = 1;
- cnt ++;
- while(rear!=front)
- {
- //把点拿出来操作
- v = Q[front++];
- for(p = G->Head[v].FirstEdge;p;p = p->Next)
- {
- if(!Visited[p->V]) {
- Q[rear++] = p->V;
- Visited[p->V] =1;
- cnt ++;
- tail = p->V;
- }
- }
- if(v==last)
- {
- level ++;
- last = tail;
- }
- if(level == 6) break;
- }
- printf("%d: %.2lf%%\n",s,cnt*1.0/G->Nv*100);
- }
- void CreateGraph()
- {
- int i;
- int v,w;
- G = (Graph)malloc(sizeof(struct GNode));
- scanf("%d%d",&G->Nv,&G->Ne);
- for(i=0;i<G->Nv;i++)
- G->Head[i].FirstEdge = NULL;
- for(i=0;i<G->Ne;i++)
- {
- scanf("%d%d",&v,&w);
- Insert(v,w);
- }
- }
- int main()
- {
- int i;
- CreateGraph();
- for(i=1;i<=G->Nv;i++)
- {
- memset(Visited,0,sizeof(Visited));
- BFS(i);
- }
- return 0;
- }
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。