Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1869
Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents
Problem description in 1967, the famous American sociologist Stanley milgranm proposed a famous hypothesis named "small world phenomenon, A maximum of six people can be associated with each other with only six people, therefore, his theory is also called six degrees of separation ). Although milgalham's theory has been often fulfilled and many scientists have always been interested in it, it has never been rigorously proven over the past 30 years, it's just a legend.
Lele is very interested in this theory, so he investigates N people in HDU. He has known the relationship between them. Now, please help him verify whether "Six-Degree separation" is true. Input this question contains multiple groups of tests, please process until the end of the file.
For each group of tests, the first line contains two integers n, m (0 <n <200, 0 <m <), representing the number of people in HDU (these are compiled into 0 ~ N-1 number) and the relationship between them.
Next, there are m rows. Each row has two integers, A and B (0 <= A, B <n), which indicate that the people numbered A and B in HDU know each other.
Except for the M group relationship, no one else knows each other.
For each group of tests, if the data conforms to the "Six-Degree separation" theory, "yes" is output in one row; otherwise, "no" is output ". Sample Input
8 70 11 22 33 44 55 66 78 80 11 22 33 44 55 66 77 0
Sample output
YesYes
Authorlinle source2008 select-a warm-up competition
Idea: you only need to set the value of the two people you know to 1, and then Floyd again. If there is a distance between two people greater than 7 (six people form seven segments ), then output "no" and vice versa "yes ".
The Code is as follows:
# Include <cstdio> # include <cstring> # define INF 99999999 # define M 217int n, m, a, B, X; int dis [m] [m]; int min (int A, int B) {int m; M = A <B? A: B; return m;} void Floyd () {for (int K = 0; k <n; k ++) {for (INT I = 0; I <N; I ++) {for (Int J = 0; j <n; j ++) {dis [I] [J] = min (DIS [I] [J], dis [I] [k] + dis [k] [J]) ;}}} void Init () {for (INT I = 0; I <N; I ++) {for (Int J = 0; j <n; j ++) {if (I = J) dis [I] [J] = 0; // This step must be classified as zero elsedis [I] [J] = inf ;}}int main () {int I, j; int cont [m]; while (~ Scanf ("% d", & N, & M) {Init (); memset (cont, 0, sizeof (cont); for (I = 0; I <m; I ++) {scanf ("% d", & A, & B ); dis [a] [B] = dis [B] [a] = 1;} Floyd (); int flag = 0; for (I = 0; I <N; I ++) {for (j = 0; j <n; j ++) {If (DIS [I] [J]> 7) {flag = 1; break ;}}if (FLAG) printf ("NO \ n"); elseprintf ("Yes \ n");} return 0 ;}