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.

53 lines
1.2 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 1010, M = N << 2; // 图的最大点数量
int n, m; // 表示图中有n个点m条边
bool st[N];
int cnt;
// 链式前向星
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c = 0) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in1.in", "r", stdin);
// freopen("in2.in", "r", stdin);
#endif
// 初始化链式前向星
memset(h, -1, sizeof h);
// 采用邻接表建图
cin >> n >> m;
// m条边
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
add(a, b);
}
// 利用bfs进行检查是不是强连通的
// 把1号结点放入队列
queue<int> q;
q.push(1);
while (q.size()) {
int u = q.front();
q.pop();
st[u] = 1;
cnt++;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (!st[v])
q.push(v);
}
}
if (cnt == n)
printf("图是连通的\n");
else
printf("图不是连通的\n");
return 0;
}