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.

45 lines
1.1 KiB

2 years ago
#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++;
}
2 years ago
int cnt[N]; // 从顶点1开始到其他每个点的最短路有几条
int dis[N]; // 最短距离
2 years ago
int n, m;
void bfs() {
2 years ago
memset(dis, 0x3f, sizeof dis);
2 years ago
queue<int> q;
q.push(1);
2 years ago
cnt[1] = 1; // 从顶点1开始到顶点1的最短路有1条
dis[1] = 0; // 距离为0
2 years ago
while (q.size()) {
int u = q.front();
q.pop();
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
2 years ago
if (dis[j] > dis[u] + 1) {
dis[j] = dis[u] + 1;
2 years ago
q.push(j);
cnt[j] = cnt[u];
2 years ago
} else if (dis[j] == dis[u] + 1)
2 years ago
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;
}