认识的人之间建立一条权值为1的边,然后求出各对顶点之间的最短路判断是否有长度大于7的。
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int INF = 999999; 8 const int N = 100; 9 int g[N][N]; 10 int n, m; 11 12 void init() 13 { 14 for ( int i = 0; i < n; i++ ) 15 { 16 for ( int j = 0; j < n; j++ ) 17 { 18 g[i][j] = INF; 19 } 20 } 21 } 22 23 void floyd() 24 { 25 for ( int k = 0; k < n; k++ ) 26 { 27 for ( int i = 0; i < n; i++ ) 28 { 29 for ( int j = 0; j < n; j++ ) 30 { 31 if ( g[i][k] + g[k][j] < g[i][j] ) 32 { 33 g[i][j] = g[i][k] + g[k][j]; 34 } 35 } 36 } 37 } 38 } 39 40 void judge() 41 { 42 for ( int i = 0; i < n; i++ ) 43 { 44 for ( int j = 0; j < n; j++ ) 45 { 46 if ( i == j ) continue; 47 if ( g[i][j] > 7 ) 48 { 49 printf("No "); 50 return ; 51 } 52 } 53 } 54 printf("Yes "); 55 } 56 57 int main () 58 { 59 while ( scanf("%d%d", &n, &m) != EOF ) 60 { 61 init(); 62 while ( m-- ) 63 { 64 int a, b; 65 scanf("%d%d", &a, &b); 66 g[a][b] = g[b][a] = 1; 67 } 68 floyd(); 69 judge(); 70 } 71 return 0; 72 }