53 lines
1.2 KiB
C++
53 lines
1.2 KiB
C++
#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;
|
||
} |