Files
python/TangDou/LanQiaoBei/LanQiaoBei13ShengSai_ZJ_II/5_bfs_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

48 lines
819 B
C++

#include <bits/stdc++.h>
/**
5 4
1 2
1 3
2 4
2 5
答案
2 1 3 2
*/
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 110;
int n, m;
int g[N][N];
int f[N];
bool st[N];
void bfs() {
queue<int> q;
q.push(n);
st[n] = true;
while (q.size()) {
auto u = q.front();
q.pop();
for (int i = 1; i < n; i++)
if (!st[i] && g[u][i] != INF) {
f[i] = f[u] + 1;
q.push(i);
st[i] = true;
}
}
}
int main() {
memset(g, 0x3f, sizeof g); // 地图初始化
cin >> n >> m;
while (m--) {
int a, b;
cin >> a >> b;
g[a][b] = g[b][a] = 1;
}
bfs();
for (int i = 1; i < n; i++) cout << f[i] << " ";
return 0;
}