#HDU 1869 six degrees of separation (floyed template title)

Problem Description

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 to the effect: given the relationship between several people, AB means that A and B know, if this figure of between everyone know if A B, B know C, then there is a degree between A and C, obtained are <= 6

Ideas: the right of every person between the value is set to 1, obtained degrees from everyone with floyed, and then determine the degree of deposit does not exist> 7 (a 1-9 draw chain to know why the 7) If present, the output "No", otherwise output "Yes"

AC Code:

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e4 + 5;
const int INF = 1e8;

int dis[maxn][maxn], n, m;
bool flag;
void init() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) dis[i][j] = dis[j][i] = 0;
            else dis[i][j] = dis[j][i] = INF;
        }
    }
    flag = true;
}

int main()
{
    while (cin >> n >> m) {
        init();
        for (int i = 0; i < m; i++) {
            int u, v;
            cin >> u >> v;
            dis[u][v] = dis[v][u] = 1;
        }
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++)
                    dis[i][j] = min (dis[i][j], dis[i][k] + dis[k][j]);
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (dis[i][j] > 7) {
                    flag = false;
                    break;
                }
            }
        }
        if (flag) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43851525/article/details/91348926