You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.0 KiB
44 lines
1.0 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
const int N = 1010; // 图的最大点数量
|
|
int n, m;
|
|
|
|
// 用floyd来判断起点是否可以达到终点
|
|
int g[N][N]; // 邻接矩阵
|
|
void floyd() {
|
|
for (int k = 1; k <= n; k++)
|
|
for (int i = 1; i <= n; i++)
|
|
for (int j = 1; j <= n; j++)
|
|
g[i][j] = g[i][j] || (g[i][k] && g[k][j]);
|
|
}
|
|
|
|
int main() {
|
|
#ifndef ONLINE_JUDGE
|
|
freopen("in1.in", "r", stdin);
|
|
// freopen("in2.in", "r", stdin);
|
|
#endif
|
|
|
|
// 采用邻接矩阵建图
|
|
cin >> n >> m;
|
|
// m条边
|
|
for (int i = 1; i <= m; i++) {
|
|
int a, b;
|
|
cin >> a >> b;
|
|
// 双向建边
|
|
g[a][b] = 1, g[b][a] = 1;
|
|
}
|
|
// 调用floyd
|
|
floyd();
|
|
|
|
for (int i = 1; i <= n; i++)
|
|
for (int j = 1; j <= n; j++)
|
|
if (!g[i][j]) {
|
|
printf("图不是连通的\n");
|
|
cout << i << " " << j << endl;
|
|
exit(0);
|
|
}
|
|
printf("图是连通的\n");
|
|
return 0;
|
|
} |