06-图3 六度空间
我用DEV-C++测过用例,通过了,可是提交到PAT上全都是段错误,今天是没办法了。花了一整天,实在是不行,求高人指点啊!
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdbool.h> 4 #include <string.h> 5 6 typedef struct node 7 { 8 int v; 9 struct node * next; 10 }Node, * pNode; 11 typedef struct 12 { 13 pNode * adjList; 14 int n; 15 bool * visited; 16 }ALGraph, * pALGraph; 17 typedef struct 18 { 19 int * elem; 20 int front, rear, size; 21 }Queue, * pQueue; 22 23 pALGraph initGraph(int N); 24 void link(pALGraph pG, int v1, int v2); 25 void insert(pNode pN, int vv); 26 int BFS(pALGraph, int vv, int N); 27 pQueue createQueue(int N); 28 bool isEmpty(pQueue pQ); 29 void inQueue(pQueue pQ, int e); 30 int outQueue(pQueue pQ); 31 32 int main() 33 { 34 // freopen("in.txt", "r", stdin); // for test 35 int i, N, M; 36 scanf("%d%d", &N, &M); 37 38 pALGraph pG; 39 pG = initGraph(N); 40 for(i = 0; i < M; i++) 41 { 42 int v1, v2; 43 scanf("%d%d", &v1, &v2); 44 link(pG, v1, v2); 45 } 46 int count; 47 for(i = 1; i <= N; i++) 48 { 49 count = BFS(pG, i, N); 50 printf("%d: %.2f%%\n", i, (float)count / N * 100); 51 } 52 // fclose(stdin); // for test 53 return 0; 54 } 55 56 pALGraph initGraph(int N) 57 { 58 pALGraph pG; 59 pG = (pALGraph)malloc(sizeof(ALGraph)); 60 pG->adjList = (pNode *)malloc((N + 1) * sizeof(pNode)); 61 pG->n = N + 1; 62 pG->visited = (bool *)malloc((N + 1) * sizeof(bool)); 63 memset(pG->visited, false, (N + 1) * sizeof(bool)); 64 for(int i = 0; i < N + 1; i++) 65 { 66 pG->adjList[i] = (pNode)malloc(sizeof(Node)); 67 pG->adjList[i]->v = i; 68 pG->adjList[i]->next = NULL; 69 } 70 71 return pG; 72 } 73 74 void link(pALGraph pG, int v1, int v2) 75 { 76 insert(pG->adjList[v1], v2); 77 insert(pG->adjList[v2], v1); 78 } 79 80 void insert(pNode pN, int vv) 81 { 82 pNode tmp = (pNode)malloc(sizeof(Node)); 83 tmp->v = vv; 84 tmp->next = pN->next; 85 pN->next = tmp; 86 // free(tmp); 87 } 88 89 int BFS(pALGraph pG, int vv, int N) 90 { 91 pQueue pQ; 92 pQ = createQueue(N); 93 94 int i, count, level, last, tail; 95 pG->visited[vv] = true; 96 count = 1; 97 level = 0; 98 last = vv; 99 inQueue(pQ, vv); 100 while(!isEmpty(pQ)) 101 { 102 vv = outQueue(pQ); 103 pNode pN = pG->adjList[vv]->next; 104 while(pN) 105 { 106 if(!pG->visited[pN->v]) 107 { 108 pG->visited[pN->v] = true; 109 count++; 110 inQueue(pQ, pN->v); 111 tail = pN->v; 112 } 113 pN = pN->next; 114 } 115 if(vv == last) 116 { 117 level++; 118 last = tail; 119 } 120 if(level == 6) 121 break; 122 } 123 memset(pG->visited, false, (N + 1) * sizeof(bool)); 124 125 return count; 126 } 127 128 pQueue createQueue(int N) 129 { 130 pQueue pQ; 131 pQ = (pQueue)malloc(sizeof(Queue)); 132 pQ->elem = (int *)malloc((N + 1) * sizeof(int)); 133 pQ->front = pQ->rear = 0; 134 pQ->size = N + 1; 135 } 136 137 bool isEmpty(pQueue pQ) 138 { 139 if(pQ->front != pQ->rear) 140 return false; 141 else 142 return true; 143 } 144 145 void inQueue(pQueue pQ, int e) 146 { 147 pQ->rear = (pQ->rear + 1) % pQ->size; 148 pQ->elem[pQ->rear] = e; 149 } 150 151 int outQueue(pQueue pQ) 152 { 153 pQ->front = (pQ->front + 1) % pQ->size; 154 return pQ->elem[pQ->front]; 155 }
“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图6.4所示。
图6.4 六度空间示意图
“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。
假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。
输入格式说明:
输入第1行给出两个正整数,分别表示社交网络图的结点数N (1<N<=104,表示人数)、边数M(<=33*N,表示社交关系数)。随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个结点的编号(节点从1到N编号)。
输出格式说明:
对每个结点输出与该结点距离不超过6的结点数占结点总数的百分比,精确到小数点后2位。每个结节点输出一行,格式为“结点编号:(空格)百分比%”。
样例输入与输出:
序号 | 输入 | 输出 |
1 |
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% |
2 |
10 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 9 10 |
1: 70.00% 2: 80.00% 3: 80.00% 4: 80.00% 5: 80.00% 6: 80.00% 7: 80.00% 8: 70.00% 9: 20.00% 10: 20.00% |
3 |
11 10 1 2 1 3 1 4 4 5 6 5 6 7 6 8 8 9 8 10 10 11 |
1: 100.00% 2: 90.91% 3: 90.91% 4: 100.00% 5: 100.00% 6: 100.00% 7: 100.00% 8: 100.00% 9: 100.00% 10: 100.00% 11: 81.82% |
4 |
2 1 1 2 |
1: 100.00% 2: 100.00%
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?