Files
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

49 lines
1.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 1010, M = N << 2; // 图的最大点数量
// 链式前向星
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 n, m; // 表示图中有n个点,m条边
bool st[N];
int cnt;
// 深度遍历
void dfs(int u) {
st[u] = 1;
cnt++; // 多走了一个结点
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (!st[v])
dfs(v);
}
}
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);
}
// 利用dfs进行检查是不是强连通的
dfs(1);
if (cnt == n)
printf("图是连通的\n");
else
printf("图不是连通的\n");
return 0;
}