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, 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
Test instructions
The relationship between each person and any other person is no more than seven. Whether the data given in the question satisfies the conditions.
Exercises
Traverse to find out the relationship distance between each person and everyone else, and judge whether the relationship distance between the person and other person is greater than seven per traversal.
If everyone satisfies the conditional output yes, as long as one person is not satisfied, stop traversing and output No.
The code is as follows:
#include <stdio.h> #include <string.h> #define INF 0x3f3f3f3f int mp[210][210];
int vis[210];
int dis[210];
int main () {int n,m;
while (~SCANF ("%d%d", &n,&m)) {int i,j,x,y,flag=0;
for (i=0;i<n;i++) for (j=0;j<n;j++) {if (i==j) mp[i][j]=0;
else Mp[i][j]=inf;
} for (i=0;i<m;i++) {scanf ("%d%d", &x,&y);
Mp[x][y]=1;
Mp[y][x]=1;
} for (i=0;i<n;i++)//traverse each person and other person's relationship distance {int tx=i,k;
memset (VIS);//Note that each traversal is initialized with the tag memset (dis,0,sizeof (dis));
for (j=0;j<n;j++) dis[j]=mp[tx][j];
dis[tx]=0;
Vis[tx]=1;
for (j=1;j<n;j++)//Ask the current person and other people's relationship distance {int minn=inf;
int u,v;
for (k=0;k<n;k++) { if (!vis[k] && minn>dis[k]) {minn=dis[k];
U=k;
}} vis[u]=1;
for (v=0;v<n;v++) {if (Mp[u][v]<inf && dis[v]>dis[u]+mp[u][v])
DIS[V]=DIS[U]+MP[U][V];
}} for (j=0;j<n;j++) {if (dis[j]>7)///As long as there is an unsatisfied on the cutoff
{flag=1;
Break
}} if (flag) break;
} if (flag) printf ("no\n");
else printf ("yes\n");
}
}