Day4 - C - six degrees of separation HDU - 1869

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 title has 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 

idea: multi-source multiple sinks, direct Floyd then judged whether the distance between each point to greater than 7, as follows:
const int maxm = 110;
const int INF = 0x7fffffff;

int N, M, G[maxm][maxm];

void init() {
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            if(i == j)
                G[i][i] = 0;
            G[i][j] = INF;
        }
    }
}

int main() {
    while(scanf("%d%d",&N,&M) != EOF) {
        init();
        for (int i = 0; i < M; ++i) {
            int t1, t2;
            scanf("%d%d", &t1, &t2);
            G[t1][t2] = G[t2][t1] = 1;
        }
        for (int k = 0; k < N; ++k) {
            for (int i = 0; i < N; ++i) {
                for(int j = 0; j < N; ++j) {
                    if(G[i][k] < INF && G[k][j] < INF)
                        G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
                }
            }
        }
        bool flag = true;
        for(int i = 0; i < N-1; ++i)
            for(int j = i+1; j < N;++j) {
                if(G[i][j] > 7) {
                    flag = false;
                    break;
                }
            }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/GRedComeT/p/11281988.html