PTA 5-7 (Chinese) Six-degree space (30)-figure-BFS

Source: Internet
Author: User

The "six-degree space" theory is also called the "six-degree separation (Six Degrees of Separation)" theory. This theory can be described in layman's words: "You and any stranger will not be more than six people, that is, up to five people you can know any stranger." As shown in "6.4.


Figure 6.4 Six-degree space

If you are given a social network diagram, calculate the percentage of nodes that correspond to the six-degree space theory for each node.

Input Format Description:

Enter line 1th to give two positive integers, representing the number of nodes in the social network graph N (1<n<=104, number of people), the number of sides m (<=33*n, which represents the social relationship). The subsequent M-line corresponds to the M-bar, and each row gives a pair of positive integers, each of which is the number of the two nodes directly connected to the bar (the node is numbered from 1 to n).

Output Format Description:

The percentage of the total number of nodes that each node output is not more than 6 from the node, and is accurate to 2 digits after the decimal point. Each node outputs a row in the form "node number: (space) percent%".

Input Sample:
10 91 22 33 44 55 66 77 88 99 10
Sample output:
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%
parsing: []
Code:
 #include <iostream> #include <fstream> #include <cstdio> #include <queue> #include <vector >using namespace Std;const int maxn=10005;int vertices, edges;vector<int> g[maxn];// The elements of each vector structure are represented: nodes with edges represented by an array subscript have a node bool VIS[MAXN]; Whether to access the int BFS (int v) {for (int i=0; i<maxn; i++) vis[i]=false;int tail;//to record the node int last=v per lamination; Record the last element of each layer: the last=temp is updated before it pops up: int count=1;int level=0;vis[v]=true;queue<int> Q;q.push (v); Q.empty ()) {int X=q.front ();//Eject x Q.pop (); for (int j=0; j<g[x].size (); j + +)//x a lap in the queue {if (!vis[g[x][j]])// If not accessed: Access and press into queue {Vis[g[x][j]]=true;q.push (g[x][j]); tail=g[x][j];//record the Node count++;}}    if (x==last)//Layer all pops up, ready to start playing the next layer: popup (x) = The last element of the current layer {last=tail; After the first layer is all pressed, update the last level++; }if (level==6) break; return count;} 
int main (int argc, char** argv) {int x,y;cin >> vertices >> edges;//ifstream fin ("test.txt");//fin >> ve Rtices >> edges;for (int i=1; i<=edges; i++) {cin >> x >> y;//fin >> x >> y; G[x].push_back (y); G[y].push_back (x);} for (int j=1; j<=vertices; J + +) {//cout << BFs (j) << endl;printf ("%d:%.2lf%%\n", J, BFs (j) *1.0/vertices* 100.0); Format output}return 0;}

PTA 5-7 (Chinese) Six-degree space (30)-figure-BFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.