题面
https://www.luogu.org/problem/P1144
题解
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int mod=100003; int n,m,dis[1050000],f[1050000]; bool vis[1050000]; vector <int> to[1050000]; queue<int> que; int main(){ int i,u,v,l; scanf("%d %d",&n,&m); for (i=1;i<=m;i++) { scanf("%d %d",&u,&v); to[u].push_back(v); to[v].push_back(u); } f[1]=1; dis[1]=0; memset(vis,0,sizeof(vis)); vis[1]=true; que.push(1); while (!que.empty()) { u=que.front(); que.pop(); l=to[u].size(); for (i=0;i<l;i++) if (!vis[to[u][i]]) { vis[to[u][i]]=true; dis[to[u][i]]=dis[u]+1; f[to[u][i]]=f[u]; que.push(to[u][i]); } else if (vis[to[u][i]] && dis[to[u][i]]==dis[u]+1) f[to[u][i]]=(f[to[u][i]]+f[u])%mod; } for (i=1;i<=n;i++) printf("%d ",f[i]); }