Six Degrees of Separation
时间限制: 1 Sec 内存限制: 33 MB[提交][状态][讨论版]
题目描述
In 1967, the famous sociologist Stanley Milgram proposed a "small world phenomenon (small world phenomenon)" the famous hypothesis,there are no more than 6 people among any two strangers, only six people can link them together. therefore his theory is also called "six degrees of separation" theory (six degrees of separation). Although Milgram's theory has often come true, there are many sociologists of interest, but in the past 30 years, it has not been rigorous proof, just a legendary hypothesis.
Lele was quite interested in the theory, so he conducted a survey of N individuals . He has got the acquaintance between them. Now, please help him to verify whether the "six degree separation" is set up.
Lele was quite interested in the theory, so he conducted a survey of N individuals . He has got the acquaintance between them. Now, please help him to verify whether the "six degree separation" is set up.
输入
This topic contains multiple sets of tests. Please handle the end of the file.
For each group of tests, the first line contains two integers N, M (0<N<100,0<M<200), representing the number of people, respectively, and the relationship between them.
Next, there are M lines, two integer A per line, B (0<=A, B<N), which means that people , numbered with A and numbered B, know each other.
In addition to this M group, the other two people are not acquainted with each other.
<n<100,0<m<200),�ֱ�����hdu������������Щ�˷ֱ�����0~n-1��)���Լ�����֮���Ĺ�ϵ��
<n)��ʾhdu������Ϊa�ͱ���b���˻�����ʶ��
For each group of tests, the first line contains two integers N, M (0<N<100,0<M<200), representing the number of people, respectively, and the relationship between them.
Next, there are M lines, two integer A per line, B (0<=A, B<N), which means that people , numbered with A and numbered B, know each other.
In addition to this M group, the other two people are not acquainted with each other.
<n<100,0<m<200),�ֱ�����hdu������������Щ�˷ֱ�����0~n-1��)���Լ�����֮���Ĺ�ϵ��
<n)��ʾhdu������Ϊa�ͱ���b���˻�����ʶ��
输出
For each test, if the data is in accordance with the "six degrees of separation in line output" Yes ", otherwise output" No".
样例输入
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
样例输出
Yes
Yes
题目分析:图论,最短路径问题,大概意思是若两人之间认识的人大于6个输出No,否则输出Yes
代码如下:
# include<stdio.h>
# include<string.h>
int main(){
int n,m;
while(~scanf("%d%d",&n,&m))
{
int a[105][105];
memset(a,100,sizeof(a));
int c1,c2;
for(int i=0;i<m;i++)
{
scanf("%d%d",&c1,&c2);
a[c1][c2]=a[c2][c1]=1; //将两人之间的距离设为 1
}
for(int k=0;k<n;k++) //克鲁斯卡尔算法
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(a[i][j]>a[i][k]+a[k][j])
a[i][j]=a[i][k]+a[k][j];
}
int k=1;
for(int i=0;i<n;i++) //判断两人i j 之间的距离是否大于6
{
for(int j=0;j<n;j++)
{
if(i!=j&&a[i][j]>6)
{
k=0;
break;
}
}
}
if(k) printf("Yes\n");
else printf("No\n");
}
return 0;
}