PTA数据结构与算法题目集(中文) 7-7

PTA数据结构与算法题目集(中文)  7-7

7-7 六度空间 (30 分)
 

“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。


图1 六度空间示意图

“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。

假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。

输入格式:

输入第1行给出两个正整数,分别表示社交网络图的结点数N(1,表示人数)、边数M(≤,表示社交关系数)。随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个结点的编号(节点从1到N编号)。

输出格式:

对每个结点输出与该结点距离不超过6的结点数占结点总数的百分比,精确到小数点后2位。每个结节点输出一行,格式为“结点编号:(空格)百分比%”。

输入样例:

10 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

输出样例:

1: 70.00%
2: 80.00%
3: 90.00%
4: 100.00%
5: 100.00%
6: 100.00%
7: 100.00%
8: 90.00%
9: 80.00%
10: 70.00%
题目分析:刚开始看我以为是得利用最短路径的想法来解 但其实是利用广度优先遍历来 计算 对于每一个图节点都使用一次广度优先遍历 记录每层的节点数 当一层的节点数随着队列的弹出减小到0时 进入下一层
注意:弹出层中的每个元素时 先进行对该元素 直接相连的元素入栈 再进行层数的判断
  1 #define _CRT_SECURE_NO_WARNINGS   
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #include<malloc.h>
  5 #define MaxVertexNum 1010
  6 
  7 typedef struct ENode* Edge;
  8 struct ENode
  9 {
 10     int V1, V2;
 11 };
 12 
 13 typedef struct GNode* Graph;
 14 struct GNode
 15 {
 16     int Nv;
 17     int Ne;
 18     int G[MaxVertexNum][MaxVertexNum];
 19 };
 20 
 21 Graph BuildGraph(int VertexNum)
 22 {
 23     Graph Gra = (Graph)malloc(sizeof(struct GNode));
 24     Gra->Ne = 0;
 25     Gra->Nv = VertexNum;
 26     for (int i = 1; i < Gra->Nv; i++)
 27         for (int j = 1; j < Gra->Nv; j++)
 28             Gra->G[i][j] = 0;
 29     return Gra;
 30 }
 31 
 32 void Insert(Edge E, Graph Gra)
 33 {
 34     Gra->G[E->V1][E->V2] = 1;
 35     Gra->G[E->V2][E->V1] = 1;
 36 }
 37 
 38 Graph CreateGraph()
 39 {
 40     Edge E;
 41     int N, M;
 42     scanf("%d%d", &N, &M);
 43     Graph G = BuildGraph(N);
 44     G->Ne = M;
 45     for (int i = 0; i < G->Ne; i++)
 46     {
 47         E = (Edge)malloc(sizeof(struct ENode));
 48         scanf("%d%d", &(E->V1), &(E->V2));
 49         Insert(E, G);
 50     }
 51     return G;
 52 }
 53 int IsEdge(Graph G,int i,int j)
 54 {
 55     return G->G[i][j] == 1;
 56 }
 57 
 58 int Colleted[1010];
 59 int LevelSize[1010];
 60 float Sum[1010];
 61 int Queue[1010];
 62 int Front = 1;
 63 int Rear = 0;
 64 int Size = 0;
 65 void Initialize()
 66 {
 67     for (int i = 0; i < 1010; i++)
 68     {
 69         Colleted[i] = 0;
 70         LevelSize[i] = 0;
 71     }
 72     Front = 1;
 73     Rear = 0;
 74     Size = 0;
 75 }
 76 int IsEmpty()
 77 {
 78     return Size == 0;
 79 }
 80 int Succ(int num)
 81 {
 82     if (num < 1010)
 83         return num;
 84     else
 85         return 0;
 86 }
 87 void EnQueue(int num)
 88 {
 89     Rear = Succ(Rear + 1);
 90     Queue[Rear] = num;
 91     Size++;
 92 }
 93 int DeQueue()
 94 {
 95     int num = Queue[Front];
 96     Front = Succ(Front + 1);
 97     Size--;
 98     return num;
 99 }
100 
101 void BFS(Graph G, int i)
102 {
103     Initialize();
104     EnQueue(i);
105     Colleted[i] = 1;
106     Sum[i]++;
107     int level = 0;
108     LevelSize[level]++;
109     while (!IsEmpty())
110     {
111         int Tmp = DeQueue();
112         LevelSize[level]--;
113         if (level>=6)
114             break;
115         for (int j = 1; j <=G->Nv; j++)
116         {
117             if (!Colleted[j] && IsEdge(G, Tmp, j))
118             {
119                 EnQueue(j);
120                 Colleted[j]=1;
121                 Sum[i]++;
122                 LevelSize[level+1]++;
123             }
124         }
125         if (LevelSize[level]==0)
126             level++;
127     }
128 }
129 void Print(Graph G)
130 {
131     
132     for (int i = 1; i <= G->Nv; i++)
133     {
134         printf("%d: %.2f%%
",i,Sum[i]/G->Nv*100);
135     }
136 }
137 int main()
138 {
139     Graph G = CreateGraph();
140     for (int i = 1; i <= G->Nv; i++)
141         BFS(G, i);
142     Print(G);
143     return 0;
144 }
View Code
原文地址:https://www.cnblogs.com/57one/p/11585978.html