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.
48 lines
819 B
48 lines
819 B
#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;
|
|
} |