C - six degrees of separation

C - six degrees of separation

In 1967, the famous American sociologist Stanley Milgram proposed a "small world phenomenon (small world phenomenon)" famous hypothesis to the effect that, in the middle of any two strangers at most across the six people that only six people can bring them together, so his theory is also known as "six degrees of separation" theory (six degrees of separation). Although Milgram's theory repeatedly come true, there are many social scientists have been keen interest in them, but in 30 years time, it has never been too rigorous proof, just a legendary hypothesis only.

Lele this theory is quite interested, so he launched an investigation of N individuals in the HDU. He has been met relations between them, would you please help him to test "six degrees of separation" is established it.

Input

This topic contains multiple sets of test, to deal with the end of the file. For each test, the first line contains two integers N, M (0 <N < 100,0 <M <200), respectively, in the HDU number (who are knitted 0 ~ N-1 number) representative of, and the relationship between them. Then there are M rows, each row two integers A, B (0 <= A , B <N) number is represented in HDU number A and B who know each other. In addition to the M group relationships, not met between any other two.

Output

For each test, if the data is in line with "six degrees of separation" theory outputs "Yes" in a row, 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

 

Subject description:

According to the data to determine any two people do not know, whether separated for up to six people among them.

analysis:

In fact, it is to ask the shortest distance, only two people who know the path is one. Do not know the other set of infinity.

With floyd algorithm for any two of the shortest distance. Then traverse it and see if there are more than 7. Than not satisfied.

Code:

#include<iostream> 
#include<string.h>
#define min(x,y) x<y?x:y;
using namespace std;
const int INF=1000000;
int ral[106][106];
​
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                ral[i][j]=(i==j?0:INF);
            }
         } 
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(a!=b)
            ral[a][b]=ral[b][a]=1;
        }
        for(int k=0;k<n;k++)
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                ral[i][j]=min(ral[i][j],ral[i][k]+ral[k][j]);
        int no=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            {
                if(ral[i][j]>7) no=1;
            }
        if(no) cout<<"No\n";
        else cout<<"Yes\n";
    }
    return 0;
}
 

Guess you like

Origin www.cnblogs.com/studyshare777/p/12198679.html
Recommended