Files
python/TangDou/AcWing_TiGao/T3/MiniPathExtend/1134_Bfs.cpp

45 lines
1.1 KiB
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
const int MOD = 100003;
int h[N], ne[N], e[N], idx;
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int cnt[N]; // 从顶点1开始,到其他每个点的最短路有几条
int dis[N]; // 最短距离
int n, m;
void bfs() {
memset(dis, 0x3f, sizeof dis);
queue<int> q;
q.push(1);
cnt[1] = 1; // 从顶点1开始,到顶点1的最短路有1条
dis[1] = 0; // 距离为0
while (q.size()) {
int u = q.front();
q.pop();
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (dis[j] > dis[u] + 1) {
dis[j] = dis[u] + 1;
q.push(j);
cnt[j] = cnt[u];
} else if (dis[j] == dis[u] + 1)
cnt[j] = (cnt[j] + cnt[u]) % MOD;
}
}
}
int main() {
memset(h, -1, sizeof h);
scanf("%d %d", &n, &m);
while (m--) {
int a, b;
scanf("%d %d", &a, &b);
add(a, b), add(b, a);
}
bfs();
for (int i = 1; i <= n; i++) printf("%d\n", cnt[i]);
return 0;
}