In 1967, Stanley Milgram, a famous American sociologist, proposed a famous hypothesis called "Small World phenomenon", to the effect that any 2 strangers could be linked together by a maximum of 6 people, that is, only 6 people. 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, Output "Yes" in one line, otherwise output "No".
Sample Input
8 70 11 22 33 44 55 66 78 80 11 22 33 44 55 66 77 0
Sample Output
Yesyes
Find the shortest distance between any two points, using the Floyd algorithm (the cube with the complexity of n!!!) )
Recommended Blog: http://developer.51cto.com/art/201403/433874.htm
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#include<stack>#include<vector>#include<map>#include<cmath>#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#defineLL Long Longusing namespacestd;Const intinf=0x3f3f3f3f;Const intmx= -+Ten;intN,M,MAP[MX][MX];voidinit () { for(intI=0; i<n;i++) for(intj=0; j<n;j++) if(I==J) map[i][j]=0; Elsemap[i][j]=INF;}voidFloyd () { for(intk=0; k<n;k++){ for(intI=0; i<n;i++) for(intj=0; j<n;j++) if(map[i][j]>map[i][k]+Map[k][j]) MAP[I][J]=map[i][k]+Map[k][j]; }}intMain () { while(~SCANF ("%d%d",&n,&m)) {init (); while(m--){ intu,v; scanf ("%d%d",&u,&v); if(map[u][v]>1) {Map[u][v]=1; Map[v][u]=1; }} Floyd (); BOOLsign=true; for(intI=0; i<n&&sign;i++) for(intj=i+1; j<n&&sign;j++) if(map[i][j]>7) sign=false; printf ("%s\n", sign?"Yes":"No"); } return 0;}
Hangzhou Electric ACM HDU 18,696 degree separation