私人联通块我恨你
2025-07-12 20:39:43
发布于:浙江
私人联通块我造密码,你真该全价四万
全部评论 2
没事的bro,会赢的
2025-07-12 来自 浙江
12025-07-12 来自 浙江
0
知道我有多绝望吗2025-07-12 来自 浙江
0:skull:没事的bro,对他使用临接矩阵吧(多骗点)
2025-07-12 来自 浙江
0我选择ai神力
#include <iostream>
using namespace std;
const int MAXN = 1e5 + 5; // 最大节点数
const int MAXM = 2e5 * 2 + 5; // 最大边数(无向边存两次)
// 图的邻接表(静态数组实现)
int graph[MAXN][2]; // 每个节点存相邻节点,这里简单用二维数组,实际需动态管理,或用链表
int edgeCount[MAXN] = {0}; // 记录每个节点的边数
bool visited[MAXN] = {false}; // 访问标记数组
int n, m; // 节点数、边数
int componentSize = 0; // 连通块节点数量
// DFS 函数
void dfs(int node) {
visited[node] = true;
componentSize++;
// 遍历当前节点的所有邻接节点
for (int i = 0; i < edgeCount[node]; i++) {
int neighbor = graph[node][i];
if (!visited[neighbor]) {
dfs(neighbor);
}
}
}
int main() {
// 加速输入输出
ios::sync_with_stdio(false);
cin.tie(nullptr);
// 读入节点数和边数
cin >> n >> m;
// 读入边,构建邻接表
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
// 无向边,双向存储
graph[u][edgeCount[u]] = v;
graph[v][edgeCount[v]] = u;
}
// 读入要查询的两个节点
int x, y;
cin >> x >> y;
// 从 x 开始 DFS
dfs(x);
// 判断 y 是否在 x 的连通块中
if (visited[y]) {
cout << componentSize << endl;
} else {
cout << 0 << endl;
}
return 0;
}2025-07-12 来自 浙江
0😱😱😱😱😱😱还是链式前项星解决方法
2025-07-12 来自 浙江
1
有帮助,赞一个