Six degree separation
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5443 Accepted Submission (s): 2208
Problem Description1967, the famous American sociologist Stanley Milgram proposed a famous hypothesis known as the "Small World phenomenon (small earth phenomenon)", to the effect that there are only 6 people among any 2 strangers, That is, only 6 people can connect them together, so his theory is also known as the "Six degree separation" theory (six Degrees of separation). Although Milgram's theory has often been fulfilled, there have been many sociologists interested in it, but in more than 30 years, it has never been a rigorous proof, but a legendary hypothesis.
Lele was quite interested in the theory, so he investigated n individuals in the HDU. He has got the relationship between them, now ask you to help him to verify the "Six Degrees of separation" is the establishment of it.
Input This topic contains multiple sets of tests, please handle to the end of the file.
For each set of tests, the first line contains two integers, n,m (0<n<100,0<m<200), representing the number of people in the HDU (0~n-1 numbers respectively), and their relationship.
Next there are m lines, each line of two integers, a, B (0<=a,b<n), which indicates that the people who are numbered a in HDU are acquainted with each other.
In addition to this m-group relationship, no other two people are acquainted.
Output for each set of tests, if the data conforms to the "Six degree separation" theory, it Outputs "Yes" in one line, otherwise the output "No".
Sample INPUT8 70 1 1 22 3 3 4 4 5 5 6 6 7 8 8 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 0
Sample Outputyes Yes Test instructions: a maximum of six connections between any two people is called a six-degree separation of ideas: let any two people between the shortest path less than 7 can Dijkstra algorithm:
#include <stdio.h> #include <string.h> #define MAX 1000#define INF 0x3f3f3fint map[max][max],vis[max];int Low[max];int n,m;int ok;void Prime (int y,int x) {int i,j,next;int min,mincost=0;memset (vis,0,sizeof (VIS)); for (i=0;i <n;i++) {low[i]=map[y][i];} Vis[y]=1;for (i=0;i<n-1;i++) {min=inf;for (j=0;j<n;j++) {if (!vis[j]&&min>low[j]) {min=low[j]; Next=j;} } vis[next]=1; for (j=0;j<n;j++) {if (!vis[j]&&low[j]>map[next][j]+low[next]) Low[j]=map[next][j]+low[next]; }} if (Low[x]==inf| | low[x]-1>6) {ok=0;printf ("no\n");}} int main () {int J,i,s,t,l;int a,b;while (scanf ("%d%d", &n,&m)!=eof) {for (i=0;i<n;i++) {for (j=0;j<n;j++) { if (i==j) Map[i][j]=map[j][i]=0;elsemap[i][j]=inf;}} for (i=0;i<m;i++) {scanf ("%d%d", &a,&b); map[a][b]=map[b][a]=1;} Ok=1;for (j=0;j<n;j++) {for (i=0;i<n;i++) {prime (j,i); if (ok==0) break; } if (ok==0) break;} if (OK) printf ("yes\n");} return 0;}
Hdoj 18,696 degrees detach the shortest path for the longest edge between 22 edges