当前位置:   article > 正文

六度空间pta_六度空间pta,c语言编程

六度空间pta,c语言编程
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define MAXN 10005
  5. typedef struct VNode* Vertex;
  6. struct VNode {
  7. Vertex Next;
  8. int V;
  9. };
  10. typedef struct LNode {
  11. Vertex FirstEdge;
  12. }List[MAXN];
  13. typedef struct GNode* Graph;
  14. struct GNode {
  15. int Nv,Ne;
  16. List Head;
  17. };
  18. Graph G;
  19. int Visited[MAXN];
  20. void Insert(int v,int w) {
  21. //无向图双向都要插
  22. Vertex NewNode = (Vertex)malloc(sizeof(struct VNode));
  23. NewNode->V = v;
  24. NewNode->Next = G->Head[w].FirstEdge;
  25. G->Head[w].FirstEdge = NewNode;
  26. NewNode = (Vertex) malloc(sizeof(struct VNode));
  27. NewNode->V = w;
  28. NewNode->Next = G->Head[v].FirstEdge;
  29. G->Head[v].FirstEdge = NewNode;
  30. }
  31. void BFS(int s) {
  32. int Q[MAXN],front = 0,rear = 0,v,i;
  33. int tail,last = s,cnt = 0,level = 0;
  34. Vertex p;
  35. Q[rear++] = s;
  36. Visited[s] = 1;
  37. cnt ++;
  38. while(rear!=front)
  39. {
  40. //把点拿出来操作
  41. v = Q[front++];
  42. for(p = G->Head[v].FirstEdge;p;p = p->Next)
  43. {
  44. if(!Visited[p->V]) {
  45. Q[rear++] = p->V;
  46. Visited[p->V] =1;
  47. cnt ++;
  48. tail = p->V;
  49. }
  50. }
  51. if(v==last)
  52. {
  53. level ++;
  54. last = tail;
  55. }
  56. if(level == 6) break;
  57. }
  58. printf("%d: %.2lf%%\n",s,cnt*1.0/G->Nv*100);
  59. }
  60. void CreateGraph()
  61. {
  62. int i;
  63. int v,w;
  64. G = (Graph)malloc(sizeof(struct GNode));
  65. scanf("%d%d",&G->Nv,&G->Ne);
  66. for(i=0;i<G->Nv;i++)
  67. G->Head[i].FirstEdge = NULL;
  68. for(i=0;i<G->Ne;i++)
  69. {
  70. scanf("%d%d",&v,&w);
  71. Insert(v,w);
  72. }
  73. }
  74. int main()
  75. {
  76. int i;
  77. CreateGraph();
  78. for(i=1;i<=G->Nv;i++)
  79. {
  80. memset(Visited,0,sizeof(Visited));
  81. BFS(i);
  82. }
  83. return 0;
  84. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/677377
推荐阅读
相关标签
  

闽ICP备14008679号