题目:https://pintia.cn/problem-sets/1268384564738605056/problems/1281571555116650497
“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图1所示。
图1 六度空间示意图
“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。
假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。
输入格式:
输入第1行给出两个正整数,分别表示社交网络图的结点数N(1<N≤103,表示人数)、边数M(≤33×N,表示社交关系数)。随后的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%
题解:https://blog.csdn.net/weixin_30708329/article/details/96271078
代码:
#include <iostream> #include <queue> #include <iomanip> using namespace std; typedef struct listNode { int data; listNode *next; }*plist, nlist; typedef struct { plist *listArray; int graphSize; bool *visited; float *sixDegreePercent; }*pGraph, nGraph; pGraph CreateGraph(int size); void ConnectGraphVertex(pGraph pG, int vStart, int vEnd); void InsertListNode(plist pL, int vertex); int BFS(pGraph pG, int vertex); void DestoryGraph(pGraph pG); pGraph CreateGraph(int size) { pGraph pG = new nGraph; pG->graphSize = size; pG->listArray = new plist[size]; pG->visited = new bool[size]; pG->sixDegreePercent = new float[size]; for (int i = 0; i < size; i++) { pG->listArray[i] = new nlist; pG->listArray[i]->data = i; pG->listArray[i]->next = NULL; pG->visited[i] = false; pG->sixDegreePercent[i] = 0; } return pG; } void DestoryGraph(pGraph pG) { plist tempListHead; plist tempListNode; for (int i = 0; i < pG->graphSize; i++) { tempListHead = pG->listArray[i]; while ( tempListHead != NULL ) { tempListNode = tempListHead; tempListHead = tempListHead->next; delete tempListNode; } } delete[]pG->listArray; delete[]pG->visited; delete[]pG->sixDegreePercent; delete pG; } void InsertListNode(plist pL, int vertex) { plist temp = new nlist; temp->data = vertex; temp->next = pL->next; pL->next = temp; return; } void ConnectGraphVertex(pGraph pG, int vStart, int vEnd) { if ( pG == NULL || vStart < 0 || vEnd < 0 || vStart == vEnd ) { return; } //将vEnd插入vStart链表 InsertListNode(pG->listArray[vStart], vEnd); //将vStart插入vEnd链表 InsertListNode(pG->listArray[vEnd], vStart); return; } int BFS(pGraph pG, int vertex) { queue<int> que; int source; plist listIter; int lastest; int tail = vertex; int level = 0; int sum = 1; //将初始结点也算在内 int maxLayer = 6; if ( pG->visited[vertex] == false ) { que.push(vertex); pG->visited[vertex] = true; } while ( que.empty() != true && level < maxLayer ) { source = que.front(); que.pop(); //遍历与vertex相连的点 listIter = pG->listArray[source]->next; while ( listIter != NULL ) { if ( pG->visited[listIter->data] == false ) { pG->visited[listIter->data] = true; sum++; que.push(listIter->data); lastest = listIter->data; } listIter = listIter->next; } if ( tail == source ) //判断是否已经遍历完该层所有结点 { level++; tail = lastest; } } //完成遍历后,要清空visited的状态 for ( int i = 0; i < pG->graphSize; i++) { pG->visited[i] = false; } return sum; } int main() { int N, E; cin >> N >> E; pGraph pG; pG = CreateGraph(N + 1); int i; int edgeStart, edgeEnd; for (i = 0; i < E; i++) { cin >> edgeStart >> edgeEnd; edgeStart; edgeEnd; ConnectGraphVertex(pG, edgeStart, edgeEnd); } float percentage; int count; for (i = 1; i <= N; i++) { count = BFS(pG, i); percentage = (float)count / N * 100; cout << fixed << setprecision(2); cout << i << ": " << percentage << '%' << endl; } DestoryGraph(pG); return 0; }