Six degree separation
Time Limit:1000MS
Memory Limit:32768KB
64bit IO Format:%I64D &%i64u Submit Status Practice HDU 1869
Description in 1967, Stanley Milgram, a famous American sociologist, proposed a famous hypothesis known as the "Small World phenomenon (small)", to the effect that there were 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 Input
8 7 0 1 1 2 2 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 Output
Yes Yes
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N #define MAX 9999999 int n,m;
int map[n][n];
int num[n];
void Floy () {int i,j,k;
for (i=0;i<n;i++) {for (j=0;j<n;j++) {for (k=0;k<n;k++) {
if (Map[j][i]+map[i][k]<map[j][k]) {map[j][k] = Map[j][i] + map[i][k];
}}}}} int main () {int i,j;
while (scanf ("%d%d", &n,&m)!=eof) {int count = 0;
for (i=0;i<n;i++) {for (j=0;j<n;j++) {map[i][j] = MAX;
} if (i = = j) {Map[i][j] = 0;
}} int x, y;
for (i=1;i<=m;i++) {scanf ("%d%d", &x,&y);
if (x!=y) {map[x][y] = 1;
MAP[Y][X] = 1; }} floy ();
for (i=0;i<n;i++) {for (j=0;j<n;j++) {if (map[i][j]>7)
{count = 1;
Break
}} if (count = = 1) {break;
}} if (count = = 1) {printf ("no\n"); } else {printf